新写的工具类,其中有对接soap的工具类
import com.shutong.common.exception.ServiceException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class HttpsUtils {
/**
* 发送HTTPS GET请求
*
* @param urlString 请求的URL地址
* @return 响应内容
* @throws IOException
*/
public static String sendGetRequest(String urlString) throws IOException {
URL url = new URL(urlString);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
conn.disconnect();
return response.toString();
}
/**
* 发送HTTPS POST请求
*
* @param urlString 请求的URL地址
* @param payload POST请求的数据
* @return 响应内容
* @throws IOException
*/
public static String sendPostRequest(String urlString, String payload) throws IOException {
URL url = new URL(urlString);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(payload.getBytes());
os.flush();
os.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
conn.disconnect();
return response.toString();
}
public static String callSonyInterfaceQuery(String jsonPrarms) {
DefaultHttpClient httpclient = new DefaultHttpClient();
//参数分别为用户名、密码、服务器url、工作域名称
HttpPost httpPost = new HttpPost("https://baidu.com?SWEExtSourceqwe=WebService&SWEExtCmdEs=Executed&WSSOAP=1");
httpPost.setHeader("SOAPAction", "\"document/http://tengxun.com/CustomUI:QueryList\"");
httpPost.setHeader("Content-Type", "text/xml");
httpPost.setHeader("Connection", "Keep-Alive");
HttpEntity entityParam = new StringEntity(jsonPrarms, ContentType.create("application/xml", "UTF-8")); //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个
httpPost.setEntity(entityParam); //把参数添加到post请求
HttpResponse response = null;
try {
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream input = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
String le;
while ((le = br.readLine()) != null) {
result.append(le);
}
br.close();
return result.toString();
} catch (Exception e) {
throw new ServiceException("接口出错" + e);
}
}
// 示例用法
public static void main(String[] args) {
String url = "https://api.example.com/data";
try {
String response = sendGetRequest(url);
System.out.println("GET Response:");
System.out.println(response);
String postData = "key1=value1&key2=value2";
response = sendPostRequest(url, postData);
System.out.println("POST Response:");
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}