httpClient使用

测试类请求,post方式

 1     public static void main(String[] args) throws HttpException, IOException {
 2         Map<String, String> content=new HashMap<String, String>();
 3         content.put("bankName", "浦发银行");
 4         content.put("cardCode", "62109810*****91");
 5         content.put("customerName", "董*");
 6         content.put("idCode", "152323199****271X");
 7         content.put("telephone", "157273073**");
 8         Map<String, Object> map=new HashMap<String, Object>();
 9         map.put("contractname", "contractAuthorization");
10         map.put("messages",content);
11         PostMethod method = new PostMethod("http://ip:8080/hexin-contract/contract/create");
12         method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
13         method.setParameter("messages",JSON.toJSONString(content));
14         method.setParameter("contractname", "contractAuthorization");
15         HttpClient httpClient = new HttpClient();
16         int statusCode = httpClient.executeMethod(method);
17         if (statusCode == HttpStatus.SC_OK) {
18             //返回附件
19             Header[] headers = method.getResponseHeaders();
20             OutputStream os=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\aa.pdf");
21             for (Header h : headers)
22                 System.out.println(h.getName() + "------------ " + h.getValue());
23             byte[] responseBody = method.getResponseBody();// 读取为字节数组
24             os.write(responseBody, 0, responseBody.length);
25             //返回string
26 //            String response = new String(responseBody, "UTF-8");
27 //            System.out.println("response:" + response);
28         }
29     }
30     

页面解析,链接转为流

get方式

URL url = new URL(htmlUrl);
InputStream openStream = url.openStream();

post方式

PostMethod method = new PostMethod(htmlUrl);
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        method.setParameter("contractName", contractName);
        method.setParameter("content", content);
        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(method);
        InputStream openStream=null;
        if (statusCode == HttpStatus.SC_OK) {
            openStream=new ByteArrayInputStream(method.getResponseBody()); 
        }

 

转载于:https://www.cnblogs.com/snow1314/p/5643858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值