`
zysnba
  • 浏览: 185803 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

HttpClient操作,HttpPost,HttpGet,HttpDelete,HttpPut

阅读更多
使用httpclient-4.5.3jar之前要导入。

1.httpGet例子如下:

try (CloseableHttpClient httpclient = HttpClientBuilder.create().build())
{
HttpGet httpget = new HttpGet(“127.0.0.1”);
HttpResponse response = httpclient.execute(httpget);
String result = EntityUtils.toString(response.getEntity());

}

2,HttpPost例子如下:
try (CloseableHttpClient httpclient = HttpClientBuilder.create().build())
{
HttpPost httpPost = new HttpPost("127.0.0.1");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("name", "张三"));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity( formparams,"UTF-8");
httpPost.setEntity(entity);
//请求方法
HttpResponse response = httpclient.execute(httpPost);
int result =response.getStatusLine().getStatusCode();
return result;
}



3,HttpDelete例子如下:
try (CloseableHttpClient httpclient = HttpClientBuilder.create().build())
{
String url =127.0.0.1+"这里是你的条件"+"/";
HttpDelete httpDelete = new HttpDelete(url);   
HttpResponse response = httpclient.execute(httpDelete);
int result =response.getStatusLine().getStatusCode();
return result;
}

4.HttpPut例子如下:
try (CloseableHttpClient httpclient = HttpClientBuilder.create().build())
{
String url =“127.0.0.1”+"这里是你的条件"+"/";
HttpPut httpPut = new HttpPut(url);  
List<NameValuePair>formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("server_name", param.getServer_name()));

UrlEncodedFormEntity entity  = new UrlEncodedFormEntity(formparams,"UTF-8");
httpPut.setEntity(entity);
HttpResponse response = httpclient.execute(httpPut);
int result =response.getStatusLine().getStatusCode();
return result;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics