httpclient

若在测试阶段可以先用chrome里的postman做发送和接受api的测试,非常方便

httpclient  get 方法:

public byte[] doHttpsGetXmlToBytesWithCookie(HttpContext httpContext, String url) {

if (url == null && url.trim().equals("")) {
return null;
}
HttpClient httpClient = getNewHttpClient();


HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Accept", "application/*+xml;version=5.1");//反馈内容的接收方式


InputStream in = null;
try {
HttpResponse response = httpClient.execute(httpGet, httpContext);
_LOG.info("status code: {}", response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
in = entity.getContent();
byte[] byteData = input2byte(in);


return byteData;
} else {
vCloudExceptionsHandler
.customVcloudExceptions(String.valueOf(response.getStatusLine().getStatusCode()));
_LOG.info(response.getStatusLine().toString());
return null;
}


} catch (ClientProtocolException e) {
e.printStackTrace(); // To change body of catch statement use File |
// Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); // To change body of catch statement use File |
// Settings | File Templates.
} finally {
httpGet.releaseConnection();
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
return null;

}


httpclient   post方法:

public byte[] doHttpsPostXmlToBytesWithCookieAndRequestBody(HttpContext httpContext, String url,
String contentType, String requestBody) {


HttpClient httpClient = getNewHttpClient();


HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Accept", "application/*+xml;version=5.1");//反馈内容的接收方式
httpPost.addHeader("Content-Type", "application/vnd.vmware.vcloud.undeployVAppParams+xml");//发送的内容为xml格式
// Construct a string entity 发送xml格式的内容
StringEntity ent = null;
try {
ent = new StringEntity(requestBody);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Set XML entity
httpPost.setEntity(ent);


InputStream in = null;
try {
HttpResponse response = httpClient.execute(httpPost, httpContext);
if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 202) {
HttpEntity entity = response.getEntity();
in = entity.getContent();
byte[] byteData = input2byte(in);


return byteData;
} else {
vCloudExceptionsHandler
.customVcloudExceptions(String.valueOf(response.getStatusLine().getStatusCode()));
return null;
}
} catch (ClientProtocolException e) {
e.printStackTrace(); // To change body of catch statement use File |
// Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); // To change body of catch statement use File |
// Settings | File Templates.
} finally {
httpPost.releaseConnection();
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
return null;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值