码迷,mamicode.com
首页 > Web开发 > 详细

HttpURLConnection 直接发送soap消息调用webservice

时间:2016-04-27 18:42:36      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

package com.travelsky.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;


public class Test {


    private String testConn(String xml) {
        String urlString = "http://IP/3uffpWS/3uffpWebService.asmx";
        HttpURLConnection httpConn = null;
        OutputStream out = null;
        String returnXml = "";
        System.out.println("开始.......");
        try {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("IP", 端口));
            
            httpConn = (HttpURLConnection) new URL(urlString).openConnection(proxy);
            httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
            httpConn.setRequestProperty("soapActionString", "http://tempuri.org/_3uffpWS/_3uffpWebService/ReturnSmsValidateCode");
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.connect();
            out = httpConn.getOutputStream(); // 获取输出流对象
            httpConn.getOutputStream().write(xml.getBytes()); // 将要提交服务器的SOAP请求字符流写入输出流
            out.flush();
            out.close();
            int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
            String tempString = null;
            StringBuffer sb1 = new StringBuffer();
            System.out.println("code="+code);
            if (code == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
                while ( (tempString = reader.readLine()) != null ) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            } else {
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream(),"UTF-8"));
                // 一次读入一行,直到读入null为文件结束
                while ( (tempString = reader.readLine()) != null ) {
                    sb1.append(tempString);
                }
                if (null != reader) {
                    reader.close();
                }
            }
            // 响应报文
            returnXml = sb1.toString();
            System.out.println("结束.......");
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        return returnXml;
    }
    

    public static void main(String[] args) {

        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            +"<soap:Body>"
            +"<ReturnSmsValidateCode xmlns=\"http://tempuri.org/_3uffpWS/_3uffpWebService\">"
            +"<memberID>string</memberID>"
            +"<validateNumUserInput>15801286875</validateNumUserInput>"
            +"</ReturnSmsValidateCode>"
            +"</soap:Body>"
            +"</soap:Envelope>";
        
        String r = new Test().testConn(xml);
        System.out.println(r);
    }
}

HttpURLConnection 直接发送soap消息调用webservice

标签:

原文地址:http://www.cnblogs.com/zhang-boke/p/5439605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!