yuwenliang521 2017-06-09 08:31 采纳率: 0%
浏览 1434

java调用http接口问题

在第三方开店卖商品,我需要把我查到的数据用post形式发给第三方接口!!不会弄啊,
各位哥哥能帮帮我吗http://open.mengdian.com/doc/api/tag/zb_list.html

  • 写回答

2条回答 默认 最新

  • Hefei19881002 2017-06-09 08:38
    关注
    public static String postDataUTF8(Map<String,String> data, String url)
    {
    
        HttpClient httpClient = new HttpClient();
        HttpConnectionManagerParams managerParams = httpClient
                .getHttpConnectionManager().getParams();
        // 设置连接超时时间(单位毫秒)
        managerParams.setConnectionTimeout(connectionTimeout);
        // 设置读数据超时时间(单位毫秒)
        managerParams.setSoTimeout(soTimeout);
        PostMethodUTF8 postMethod = new PostMethodUTF8(url);
        Iterator<String> it = data.keySet().iterator();
    
        NameValuePair[] sendData = new NameValuePair[data.keySet().size()];
        int i = 0;
        String key = "";
        while(it.hasNext())
        {
            key = it.next();
            sendData[i++] = new NameValuePair(key, data.get(key));
        }
    
        postMethod.setRequestBody(sendData);
        postMethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); 
        String strResponse = null;
        try {
            int statusCode = httpClient.executeMethod(postMethod);
            //logger.info("--------Connect the interface at time:" + new Date());
            if (statusCode != HttpStatus.SC_OK) {
                strResponse = "error_第三方返回非200状态码";
                throw new IllegalStateException("Method failed: "
                        + postMethod.getStatusLine());
            }
            strResponse = postMethod.getResponseBodyAsString();
            if(NetCheckFieldsUtil.isBlank(strResponse)){
                strResponse = "error_第三方返回空数据";
            }
            //System.out.println("getResponseCharSet:"+postMethod.getResponseCharSet());
            //System.out.println("getRequestCharSet:"+postMethod.getRequestCharSet());
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
            logger.error("UnsupportedEncodingException:",e);
            strResponse = "error_发生UnsupportedEncodingException异常,未能成功调用第三方";
        }catch(IllegalStateException e){
            e.printStackTrace();
            logger.error("IllegalStateException:",e);
    

    // strResponse = "error_发生IllegalStateException异常,未能成功调用第三方";
    } catch (ConnectTimeoutException e) {
    // System.out.println("time out");
    e.printStackTrace();
    // throw new java.net.SocketTimeoutException("time out");
    logger.error("ConnectTimeoutException:",e);
    strResponse = "error_请求第三方时发生ConnectTimeoutException异常";
    } catch(SocketTimeoutException e){
    e.printStackTrace();
    logger.error("SocketTimeoutException:",e);
    strResponse = "error_请求第三方时发生SocketTimeoutException异常";
    }catch (Exception ex) {
    ex.printStackTrace();
    // throw new IllegalStateException(ex.toString());
    logger.error("Exception:",ex);
    strResponse = "error_请求第三方时发生未知异常";
    } finally {
    postMethod.releaseConnection();
    }
    //logger.info(strResponse);
    return strResponse;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看