Java发送GET、POST请求代码
发布时间:2020-05-24 15:43:44 所属栏目:Java 来源:互联网
导读:Java发送GET、POST请求代码
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 一、创建一个servlet来接收get或post请求packageguwen;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.io.SAXReader;
publicclassTestServletextendsHttpServlet{
/**
*接收get请求
*/
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
System.out.println(req.getQueryString());//打印参数
PrintWriterout=resp.getWriter();
out.print("响应");//响应
}
/**
*接收post请求
*/
@Override
protectedvoiddoPost(HttpServletRequestreq,IOException{
resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");
System.out.println(req.getQueryString());//打印参数
InputStreaminputStream=req.getInputStream();
SAXReaderreader=newSAXReader();
try{
Documentdocument=reader.read(inputStream);//报文体
System.out.println(document.asXML());
}catch(DocumentExceptione){
e.printStackTrace();
}
PrintWriterout=resp.getWriter();
out.print("响应");//响应
}
}
二、发送请求packageguwen;
importjava.io.IOException;
importorg.apache.http.HttpEntity;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.config.RequestConfig;
importorg.apache.http.client.methods.CloseableHttpResponse;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.entity.StringEntity;
importorg.apache.http.impl.client.CloseableHttpClient;
importorg.apache.http.impl.client.HttpClientBuilder;
importorg.apache.http.util.EntityUtils;
publicclassConnectionUtil{
publicstaticvoidmain(String[]args)throwsClientProtocolException,IOException{
//sentget
doGet("http://localhost:8088/sentTest/test?p=123");
//sentpost
doPost("http://localhost:8088/sentTest/test?p=321","<xml><a>aa</a><b>哈</b></xml>");
}
/**
*发送get请求
*@throwsIOException
*/
publicstaticStringdoGet(Stringurl)throwsClientProtocolException,IOException{
CloseableHttpClienthttpClient=HttpClientBuilder.create().build();
HttpGethttpGet=newHttpGet(url);
//配置请求的超时设置
RequestConfigrequestConfig=RequestConfig.custom()
.setConnectionRequestTimeout(50)
.setConnectTimeout(50)
.setSocketTimeout(50).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponseresponse=null;
Stringres=null;
try{
response=httpClient.execute(httpGet);//发送请求
System.out.println("StatusCode->"+response.getStatusLine().getStatusCode());
HttpEntityentity=response.getEntity();
res=EntityUtils.toString(entity,"utf-8");
System.out.println(res);
}catch(ClientProtocolExceptione){
throwe;
}catch(IOExceptione){
throwe;
}finally{
httpGet.releaseConnection();
}
returnres;
}
/**
*发送get请求
*@throwsIOException
*/
publicstaticStringdoPost(Stringurl,Stringbody)throwsIOException{
CloseableHttpClienthttpclient=HttpClientBuilder.create().build();
HttpPosthttpPost=newHttpPost(url);
//配置请求的超时设置
RequestConfigrequestConfig=RequestConfig.custom()
.setConnectionRequestTimeout(50)
.setConnectTimeout(50)
.setSocketTimeout(50).build();
httpPost.setConfig(requestConfig);
CloseableHttpResponseresponse=null;
Stringres=null;
try{
if(body!=null){//设置报文体设置编码为UTF-8
StringEntityentity=newStringEntity(body,"UTF-8");
httpPost.setEntity(entity);
}
response=httpclient.execute(httpPost);//发送请求
System.out.println(response.toString());
HttpEntityentity=response.getEntity();
res=EntityUtils.toString(entity,"utf-8");
System.out.println(res);
}catch(ClientProtocolExceptione){
throwe;
}catch(IOExceptione){
throwe;
}finally{
httpPost.releaseConnection();
}
returnres;
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
