码迷,mamicode.com
首页 > 编程语言 > 详细

解决java访问.netWebService的常见问题

时间:2016-08-02 11:25:03      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

到公司没多久,写了一个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;
    }
}
望大家有什么意见多多提议哈

解决java访问.netWebService的常见问题

标签:

原文地址:http://www.cnblogs.com/shejinrui/p/5728371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!