请求webService的几种方式

本文提供了两种使用Java发送HTTP POST请求的方法:一种是通过HttpURLConnection发送XML格式的数据;另一种是利用HttpClient库发送SOAP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种,使用HttpURLConnection,post请求。需要传入路径以及xml报文,返回结果报文。

public String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        OutputStream ous = null;
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setAllowUserInteraction(true);
            conn.setRequestProperty("content-type", "application/xml");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            ous = conn.getOutputStream();
            ous.write(param.getBytes());
            ous.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                if(ous!=null){
                    ous.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

第二种,使用client方式,post请求,我使用的是 httpclient-cache-4.1.2.jar,httpcore-4.3.jar,httpmime-4.3.1.jar。

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");  
                 System.out.println(retStr);
             }  
             // 释放资源  
             closeableHttpClient.close();  
         }catch (Exception e) {  
         }  
         return retStr;  
     }
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值