朱杰兵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);
              }
           
          }

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 田林县| 平舆县| 平乐县| 青神县| 九寨沟县| 永福县| 望都县| 滦南县| 六盘水市| 鹿泉市| 北安市| 鸡西市| 安阳市| 醴陵市| 疏附县| 闵行区| 于都县| 三河市| 和硕县| 黄石市| 吉林省| 堆龙德庆县| 平塘县| 马龙县| 东安县| 奉化市| 昌平区| 巴塘县| 五华县| 伽师县| 平湖市| 黄石市| 台江县| 黄浦区| 京山县| 扶绥县| 金秀| 伊金霍洛旗| 松桃| 都匀市| 天等县|