Java模拟HttpClient进行Get和Post提交
2015-11-10 14:55
561 查看
使用Java模拟客户端进行提交,需要用到apache http client jar,这里用的是4.4版本
GET:
public void GetURL(){
String strResp="";
String url="www.baidu.com";
int statuscode=0;
CloseableHttpClient client=HttpClients.createDefault();
HttpGet request=new HttpGet(url);
try{
HttpResponse response=client.execute(request);
statuscode=response.getStatusLine().getStatusCode();
if(statuscode==200){
strResp=EntityUtils.toString(response.getEntity());//返回的内容
}
}catch(Exception e){
e.printStackTrace();
}
POST:
public String PostURL(){
String strResp="";
String url="www.baidu.com";
int statuscode=0;
CloseableHttpClient client = HttpClients.createDefault();
HttpPost request=new HttpPost(url);
JSONStringer jsonbody;
try{
jsonbody=(JSONStringer) new JSONStringer().object().key("vehicleID").value("8856017585").endObject();
StringEntity se=new StringEntity(jsonbody.toString(),"UTF-8");
se.setContentType("application/json");
request.setEntity(se); //设置要POST的内容,当前这里使用的是json格式
HttpResponse response=client.execute(request);
statuscode=response.getStatusLine().getStatusCode();
if(statuscode==HttpStatus.SC_OK){
strResp=new String(EntityUtils.toString(response.getEntity()).getBytes("UTF-8")); }
client.close();
}catch(Exception e){
e.printStackTrace(); }
return strResp; }
GET:
public void GetURL(){
String strResp="";
String url="www.baidu.com";
int statuscode=0;
CloseableHttpClient client=HttpClients.createDefault();
HttpGet request=new HttpGet(url);
try{
HttpResponse response=client.execute(request);
statuscode=response.getStatusLine().getStatusCode();
if(statuscode==200){
strResp=EntityUtils.toString(response.getEntity());//返回的内容
}
}catch(Exception e){
e.printStackTrace();
}
POST:
public String PostURL(){
String strResp="";
String url="www.baidu.com";
int statuscode=0;
CloseableHttpClient client = HttpClients.createDefault();
HttpPost request=new HttpPost(url);
JSONStringer jsonbody;
try{
jsonbody=(JSONStringer) new JSONStringer().object().key("vehicleID").value("8856017585").endObject();
StringEntity se=new StringEntity(jsonbody.toString(),"UTF-8");
se.setContentType("application/json");
request.setEntity(se); //设置要POST的内容,当前这里使用的是json格式
HttpResponse response=client.execute(request);
statuscode=response.getStatusLine().getStatusCode();
if(statuscode==HttpStatus.SC_OK){
strResp=new String(EntityUtils.toString(response.getEntity()).getBytes("UTF-8")); }
client.close();
}catch(Exception e){
e.printStackTrace(); }
return strResp; }
相关文章推荐
- App Transport Security has blocked a cleartext HTTP (http://) resource load
- iOS 开发网络数据处理 ------ AFNetWorking
- HttpWebResponse 传输IDictionary<string, string>[] parameter数组
- HTTP 状态码整理
- android GridView 通过json显示网络图片和文字
- (转)TDI FILTER 网络过滤驱动完全解析
- 搭建入门网络验证码(注册码)教程
- 如何在Ant中设置HTTP代理
- Android网络数据请求之HttpClient
- 在Spring Boot中使用Https
- Zigbee网关作为tcpclient示意图
- Meta http-equiv属性详解
- iOS网络之多线程
- 机器学习笔记——神经网络的实现
- centos7中控制节点配置网络
- centos7中控制节点配置网络
- xutil中httputils解析
- 深入浅出LSTM神经网络 ------从卷积 递归网络 到长短时间记忆模型
- 操作系统与网络实现 之九
- 将社交网络集成到 BPM,第 1 部分: 收集 Twitter 数据