JAVA 用HTTP发送SOAP报文请求(出现过报500情况)

 跟发送XML报文的请求的唯一差别:

httpConn.setRequestProperty("SOAPAction","urn:action");//添加了该请求头

至于该属性的值是多少,你们可以自行用SOAPUI工具(软件怎么使用就自行百度吧)去发送SOAP请求,看它具体发送成功的请求头信息,请求日志如下为:

请求的代码如下:

 /**
     * 发送POST请求的SOAP报文格式
     * @param SOAPUrl  请求地址
     * @param parXmlInfo 请求报文
     * @return
     */
    public static String SendSoapPost(String SOAPUrl,String parXmlInfo){
        StringBuilder result = new StringBuilder();
        OutputStream out = null;
        BufferedReader in = null;
        try {
            URL url = new URL(SOAPUrl);
            URLConnection connection = url.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection) connection;
            byte[] b = parXmlInfo.getBytes("ISO-8859-1");
            httpConn.setRequestProperty( "Content-Length",String.valueOf( b.length ) );
            httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
            httpConn.setRequestProperty("SOAPAction","urn:action");//重点中的重点,不加就500
            httpConn.setRequestMethod( "POST" );
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);

            out = httpConn.getOutputStream();
            out.write( b );
            in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                result.append(inputLine);
            out.close();
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        System.out.println("\n respone:\n"+result);
        return result.toString();
    }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值