解決java訪問.netWebService的常見問題,.netwebservice

來源:互聯網
上載者:User

解決java訪問.netWebService的常見問題,.netwebservice

到公司沒多久,寫了一個java調用.net寫的webService結果期間用各種方法測試都沒有完成,總是拋出異常,最後直接使用SOAP訊息去進行調用才成功了,具體代碼如下,僅供參考:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class QueryDataUtil {
    public static String getOpdetailString(String regoinId,String regoinName,String signalName) throws Exception{
        String xml ="";
        xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xml +="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
        xml +="<soapenv:Header/>";
        xml +="<soapenv:Body>";
          xml += "<tem:QueryDeviceRealData><tem:regionId>"+regoinId+"</tem:regionId><tem:regionName>"+regoinName+"</tem:regionName><tem:signalName>"+signalName+"</tem:signalName></tem:QueryDeviceRealData>";
          String xml1="</soapenv:Body>";
             xml1+="</soapenv:Envelope>";
             xml +=xml1;
             String urlString="http://IP:連接埠/xxxx?wsdl";
              HttpURLConnection httpConn = null;
           OutputStream out = null;
           String returnXml="";
           //設定批次號
               DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
              String s= df.format(new Date());
              System.out.println("開始");
            httpConn = (HttpURLConnection) new URL(urlString).openConnection();
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            httpConn.setRequestProperty("SOAPAction", "xxx");
            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();
            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("returnOpdetail==="+returnXml);
            return returnXml;
    }
}
望大家有什麼意見多多提議哈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.