使用HttpUrlConnection訪問webservice

SOAP

1.復制webservice頁面的soap數(shù)據,例如下圖:

webservice soap

string用具體的數(shù)值代替

2.代碼

String soapRequestData = "<?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:Header>" + "   <RoxvellWebserviceHeader xmlns=\"http://tempuri.org/\">" + "    <UserName>"
            + userName          
            + "      <pMapFloorDateTime>" + "" + "</pMapFloorDateTime>" + "    </getMasterV2>" + "  </soap:Body>" + "</soap:Envelope>";

    URL url;
    try {
        url = new URL(WebService.GetMasterV2);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");

        OutputStream os = conn.getOutputStream();

        os.write(soapRequestData.getBytes());

        InputStream is = conn.getInputStream();
                    StringBuilder sb=new StringBuilder();
        BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));
        String line="";
        while((line=br.readLine())!=null){
            sb.append(line);
        }       

        is.close();
        os.close();
        conn.disconnect();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

記得開啟新線程

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,626評論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 一. Java基礎部分.................................................
    wy_sure閱讀 3,995評論 0 11
  • 一、Java基礎 1.寫出下面代碼的執(zhí)行結果 2.寫出下面代碼的執(zhí)行結果 3.寫出下面代碼的執(zhí)行結果 (此題需寫出...
    joshul閱讀 577評論 0 1
  • 2016年10月14日,”2016安亭國際汽車金融論壇”在上海奕穎皇冠假日酒店開幕,本次論壇由上海汽車金融港、建元...
    WeClub閱讀 532評論 0 3

友情鏈接更多精彩內容