1.引入maven依賴:java
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>web
2.請求工具類:spring
package com.hanvon.iface.web.utils;
import org.apache.http.HttpEntity;
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;
import org.apache.log4j.Logger;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
/**
* webservice發送請求工具類
* Author:lsf
* Date:19-01-08
*/
public class WebServiceUtil {
static int socketTimeout = 30000;// 請求超時時間
static int connectTimeout = 30000;// 傳輸超時時間
static Logger logger = Logger.getLogger(WebServiceUtil.class);
/**
* 使用SOAP發送消息(HttpClient方式)
*
* @param postUrl
* @param soapXml
* @param soapAction
* @return
*/
public static String doPostSoap(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 建立HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 設置請求和傳輸超時時間
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印響應內容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("response:" + retStr);
}
// 釋放資源
closeableHttpClient.close();
} catch (Exception e) {
logger.error("請求出錯!", e);
}
return retStr;
}
/**
* 使用HttpURLConnection方式鏈接
* @param soapurl
* @param soapXML
* @return
* @throws IOException
*/
public static String urlConnectionUtil(String soapurl,String soapXML) throws IOException {
//第一步:建立服務地址
//http://172.25.37.31:8080/PeopleSoftService/getPerson.wsdl
URL url = new URL(soapurl);
//第二步:打開一個通向服務地址的鏈接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:設置參數
//3.1發送方式設置:POST必須大寫
connection.setRequestMethod("POST");
//3.2設置數據格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3設置輸入輸出,由於默認新建立的connection沒有讀寫權限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:組織SOAP數據,發送請求
//將信息以流的方式發送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
StringBuilder sb = new StringBuilder();
//第五步:接收服務端響應,打印
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服務端響應成功
//獲取當前鏈接請求返回的數據流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
is.close();
isr.close();
br.close();
}
os.close();
return sb.toString();
}apache
}springboot
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------app
springboot中經過RestTemplate調用socket
@SpringBootApplication
@ServletComponentScan
public class Springboot02 extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Springboot02.class, args);
}
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestControllerpublic class RService { @Autowired private RestTemplate restTemplate; @RequestMapping("/weather") public String get(){ String string=restTemplate.getForEntity("http://localhost:8080/test",String.class).getBody(); return string; }}