使用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包:

 

posted on   SmartTony_07  阅读(108)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示