首页 > 编程语言 >使用JavaHTTPClient发送请求

使用JavaHTTPClient发送请求

时间:2022-12-08 14:12:06浏览次数:47  
标签:xml http 请求 发送 httpPost org apache import JavaHTTPClient

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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 Main {
    public static void main(String[] args){
        String l_xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tip=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <tip:OperaNorPostRequest>\n" +
                "         <tip:request><![CDATA[<Request>\n" +
                " <Access>\n" +
                "  <Authentication user='xxxx' password=''></Authentication>\n" +
                "  <Organization name='"+args[1]+"'></Organization>\n" +
                " </Access>\n" +
                " <RequestContent>\n" +
                "     <Parameter>\n" +
                "   <Record>\n" +
                "    <Field name='type' value='DBSplit'/>                             \n" +
                "    <Field name='rec' value='"+args[0]+"'/>         \n" +
                "   </Record>\n" +
                "  </Parameter>\n" +
                " </RequestContent>\n" +
                "</Request>]]></tip:request>\n" +
                "      </tip:OperaNorPostRequest>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        //System.out.println(l_xml);
        //创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        //创建HTTPClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        //创建HTTPPOST
        HttpPost httpPost = new HttpPost("http://xxx.xxx.xxx.xxx/web/ws/r/aws_ttsrv2");
        //设置超时时间
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000*30).setConnectTimeout(1000*30).build();
        httpPost.setConfig(requestConfig);
        System.out.println("配置完成");
        try{
            //配置post请求头
            httpPost.setHeader("Content-Type","application/xml");
            httpPost.setHeader("SOAPAction","\"\"");
            httpPost.setHeader("Connection","keep-alive");
            httpPost.setHeader("Accept-Encoding","gzip,deflate");
            //配置post请求体
            StringEntity data = new StringEntity(l_xml,"UTF-8");
            data.setContentType("application/xml");
            httpPost.setEntity(data);
            //发送请求
            CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
            //获取响应体
            HttpEntity httpEntity = response.getEntity();
            System.out.println(response.getStatusLine().getStatusCode());
            if(httpEntity != null){
                String retStr = EntityUtils.toString(httpEntity,"UTF-8");
                System.out.println(retStr);
            }
            closeableHttpClient.close();
        }catch (Exception e){
            System.out.println(e.toString());
        }
    }
}

依赖jar包:

 

标签:xml,http,请求,发送,httpPost,org,apache,import,JavaHTTPClient
From: https://www.cnblogs.com/smarttony/p/16965927.html

相关文章

  • ORA-12514 监听程序当前无法识别连接描述符中请求
    问题描述:在安装完数据库后,无法进行正常连接,提示ORA-12514错误,环境为WindowsServer2008R2+Oracle11.2.0.4,之前安装11.2.0.1的时候没有遇到这个问题,不清楚是否和版本有关。......
  • Java Web服务器是怎么处理请求的?
    从2017年初开始自学Java,到现在工作一年半,恍然间已经在Java世界里畅游了大概5年。作为一名Java后端程序员,如今日常工作就是写写接口,用来接收前端的请求,然后返回处理结果。......
  • HTTP_请求消息_请求行和HTTP_请求消息_请求头&请求体
    HTTP_请求消息_请求行:请求消息数据格式1.请求行请求行:请求方式 请求url 请求协议/版本GET/login.htmlHTTP/1.1     HTTP_请求消息_请求......
  • Springboot处理跨域请求
    Springboot中如何处理跨域请求一.什么是跨域?我们知道Url的一般格式:协议+域名(子域名+主域名)+端口号+资源地址比如:https://www.itquanmingxing.c......
  • Postman(一): postman介绍和安装,发送带参数的GET请求
    Postman(1):postman的介绍和安装Postman的介绍Postman是一款谷歌开发的接口测试工具,使API的调试与测试更加便捷。它提供功能强大的WebAPI&HTTP请求调试。它能够发......
  • 用C#发送post请求,实现更改直播间标题[简单随笔]
    第一次发这样的网络数据包。记录一下。API参考https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/live/manage.md做了很多尝试才成功,遇到最大的困......
  • Java前后端请求Content-Type与接受方式
    1.GetGet方法没有请求体,所以加不加Content-Type没有意义。参数通过拼接到Url来加入url?key=value&key2=value2SpringMVC后台如何获取参数:Java后台通过Request的get......
  • 发送短信验证码
    1<inputid="sendBtn"type="button"value="点击获取验证码"class="btnbtn-default"/>2<buttontype="button"class="btnbtn-primary"id="loginBtn">登录</bu......
  • http post请求超时
    1.请求的高德的api2.本地测试,httpclient和okhttp都用过,本地没有问题,但是部署在客户的内网服务器上,就会有几率出现读取超时的情况,这个是截图 3.我在服务器上也pin......
  • 原生Ajax发送请求
    GET//1.创建一个xmlhttpRequest对象varxmlhttp=null;varres;if(window.XMLHttpRequest){xmlhttp=newXMLHttpRequest();}els......