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