java对接soap工具类-callSonyInterfaceQuery

新写的工具类,其中有对接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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值