朱杰兵blog

jonhney'blog
posts - 140, comments - 1, trackbacks - 0, articles - 0

package com.what21.service.demo02;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class CallByHttp {
 
    private static String actionBySOAP(){
        StringBuilder sb = new StringBuilder();
        sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas" +
                ".xmlsoap.org/soap/envelope/\" xmlns:sms=\"helloworld\">");
        sb.append("<soapenv:Header/>");
        sb.append("<soapenv:Body>");
        sb.append("<sms:sendSMS>");
        sb.append("<sms:in0>123</sms:in0>");
        sb.append("</sms:sendSMS>");
        sb.append("</soapenv:Body>");
        sb.append("</soapenv:Envelope>");
        return sb.toString();
    }
  
    private static String callWebService(String wsdl)
            throws Exception{
        System.setProperty("sun.net.client.defaultConnectTimeout","20000");
        System.setProperty("sun.net.client.defaultReadTimeout","20000");
         
        // URL连接
        URL url = new URL(wsdl);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        // conn.setRequestMethod("GET");
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Length", String.valueOf(actionBySOAP().getBytes().length));
        conn.setRequestProperty("Content-Type","text/xml; charset=GBK");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setConnectTimeout(20000);
        // 请求输入内容
        OutputStream output = conn.getOutputStream();
        output.write(actionBySOAP().getBytes());
        output.flush();
        output.close();
        // 请求返回内容
        InputStreamReader isr = new InputStreamReader(conn.getInputStream());
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String str = null;
        while((str = br.readLine())!=null){
            sb.append(str + "\n");
        }
        br.close();
        isr.close();
        return sb.toString();
    }
  
  
    /**
    * @param args
    */
    public static void main(String[] args) throws Exception {
        String result = callWebService(WSDL);
        System.out.println(result);
    }
 
}

只有注册用户登录后才能发表评论。


网站导航: