加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java发送GET、POST请求代码

发布时间:2020-12-15 00:16:18 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 一、创建一个servlet来接收get或post请求 package?guwen;import?java.io.IOException;import?java.io.InputStream;import?java.io.PrintWriter;import

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

一、创建一个servlet来接收get或post请求

package?guwen;

import?java.io.IOException;
import?java.io.InputStream;
import?java.io.PrintWriter;

import?javax.servlet.ServletException;
import?javax.servlet.http.HttpServlet;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;

import?org.dom4j.Document;
import?org.dom4j.DocumentException;
import?org.dom4j.io.SAXReader;

public?class?TestServlet?extends?HttpServlet{

????/**
?????*?接收get请求
?????*/
????@Override
????protected?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)?throws?ServletException,?IOException?{
????????req.setCharacterEncoding("UTF-8");
????????resp.setCharacterEncoding("UTF-8");
????????System.out.println(req.getQueryString());??//打印参数
????????PrintWriter?out?=?resp.getWriter();
????????out.print("响应");??//响应
????}

????/**
?????*?接收post请求
?????*/
????@Override
????protected?void?doPost(HttpServletRequest?req,?IOException?{
????????resp.setCharacterEncoding("UTF-8");
????????req.setCharacterEncoding("UTF-8");
????????System.out.println(req.getQueryString());??//打印参数
????????InputStream?inputStream?=?req.getInputStream();
????????SAXReader?reader?=?new?SAXReader();
????????try?{
????????????Document?document?=?reader.read(inputStream);??//报文体
????????????System.out.println(document.asXML());
????????}?catch?(DocumentException?e)?{
????????????e.printStackTrace();
????????}
????????PrintWriter?out?=?resp.getWriter();
????????out.print("响应");??//响应
????}

????
}

二、发送请求

package?guwen;

import?java.io.IOException;

import?org.apache.http.HttpEntity;
import?org.apache.http.client.ClientProtocolException;
import?org.apache.http.client.config.RequestConfig;
import?org.apache.http.client.methods.CloseableHttpResponse;
import?org.apache.http.client.methods.HttpGet;
import?org.apache.http.client.methods.HttpPost;
import?org.apache.http.entity.StringEntity;
import?org.apache.http.impl.client.CloseableHttpClient;
import?org.apache.http.impl.client.HttpClientBuilder;
import?org.apache.http.util.EntityUtils;


public?class?ConnectionUtil?{
????
????public?static?void?main(String[]?args)?throws?ClientProtocolException,?IOException?{
????????//sent?get
????????doGet("http://localhost:8088/sentTest/test?p=123");
????????//sent?post
????????doPost("http://localhost:8088/sentTest/test?p=321","<xml><a>aa</a><b>哈</b></xml>");
????}
????
????/**
?????*?发送get请求
?????*?@throws?IOException?
?????*/
????public?static?String?doGet(String?url)?throws?ClientProtocolException,?IOException?{
????????
????????CloseableHttpClient?httpClient?=?HttpClientBuilder.create().build();??
????????
????????HttpGet?httpGet?=?new?HttpGet(url);?????
??
????????//配置请求的超时设置??
????????RequestConfig?requestConfig?=?RequestConfig.custom()????
????????????????.setConnectionRequestTimeout(50)??
????????????????.setConnectTimeout(50)????
????????????????.setSocketTimeout(50).build();????
????????httpGet.setConfig(requestConfig);???
??????????
????????CloseableHttpResponse?response?=?null;
????????String?res?=?null;
????????try?{
????????????response?=?httpClient.execute(httpGet);???//发送请求
????????????System.out.println("StatusCode?->?"?+?response.getStatusLine().getStatusCode());??
????????????HttpEntity?entity?=?response.getEntity();??????????
????????????res?=?EntityUtils.toString(entity,"utf-8");???
????????????System.out.println(res);??
????????}?catch?(ClientProtocolException?e)?{
????????????throw?e;
????????}?catch?(IOException?e)?{
????????????throw?e;
????????}?finally{
????????????httpGet.releaseConnection();?
????????}
??????????
????????return?res;
????}
????
????
????
????/**
?????*?发送get请求
?????*?@throws?IOException?
?????*/
????public?static?String?doPost(String?url,String?body)?throws?IOException?{
????????
????????CloseableHttpClient?httpclient?=?HttpClientBuilder.create().build();??
????????HttpPost?httpPost?=?new?HttpPost(url);
????????//配置请求的超时设置??
????????RequestConfig?requestConfig?=?RequestConfig.custom()????
????????????????.setConnectionRequestTimeout(50)??
????????????????.setConnectTimeout(50)????
????????????????.setSocketTimeout(50).build();????
????????httpPost.setConfig(requestConfig);?
????????
????????CloseableHttpResponse?response?=?null;
????????String?res?=?null;
????????try?{
????????????if?(body?!=?null)?{??//设置报文体?设置编码为?UTF-8
????????????????StringEntity?entity?=?new?StringEntity(body,?"UTF-8");
????????????????httpPost.setEntity(entity);
????????????}
????????????response?=?httpclient.execute(httpPost);??//发送请求
????????????System.out.println(response.toString());??
????????????
????????????HttpEntity?entity?=?response.getEntity();??
????????????res?=?EntityUtils.toString(entity,?"utf-8");??
????????????System.out.println(res);??
????????}?catch?(ClientProtocolException?e)?{
????????????throw?e;
????????}?catch?(IOException?e)?{
????????????throw?e;
????????}?finally{
????????????httpPost.releaseConnection();
????????}
????????
????????return?res;
????????
????????
????}
????
????
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读