使用HttpUrlConnection访问webservice

SOAP

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

使用HttpUrlConnection访问webservice_第1张图片
webservice soap

string用具体的数值代替

2.代码

String soapRequestData = ""
            + ""
            + "  " + "   " + "    "
            + userName          
            + "      " + "" + "" + "    " + "  " + "";

    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)