美文网首页
使用HttpUrlConnection访问webservice

使用HttpUrlConnection访问webservice

作者: 盛夏的阳光 | 来源:发表于2016-08-31 18:13 被阅读97次

SOAP

1.复制webservice页面的soap数据,例如下图:

webservice soap

string用具体的数值代替

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();
    }
}

记得开启新线程

相关文章

网友评论

      本文标题:使用HttpUrlConnection访问webservice

      本文链接:https://www.haomeiwen.com/subject/pjfcettx.html