使用HttpUrlConnection访问webservice

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

记得开启新线程

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,778评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 一. Java基础部分.................................................
    wy_sure阅读 3,839评论 0 11
  • 一、Java基础 1.写出下面代码的执行结果 2.写出下面代码的执行结果 3.写出下面代码的执行结果 (此题需写出...
    joshul阅读 544评论 0 1
  • 2016年10月14日,”2016安亭国际汽车金融论坛”在上海奕颖皇冠假日酒店开幕,本次论坛由上海汽车金融港、建元...
    WeClub阅读 504评论 0 3