分享

java HttpConnection 调用.NET WebService

 instl 2014-08-27

最近项目上要求调用异地的WebService,以前有写过xfire WebService,就按照原先的写了,发现掉不同,后来发现对面写的WebServcie是使用.NET语言写的,且传输的是对象,愁了三天,相继使用了XFIRE,以及CXF,AXIS调用,都不行,各种错误。后来就想使用基础的,java自带的HttpConnection提交其XML数据,后来开始也是掉不同,出现错误如下:

Java代码  收藏代码
  1. java.io.IOException: Server returned HTTP response code: 500 for URL:  
 

 

 后来从网上找原因都说是要求增加如下代码:

connectioin.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

试了不行,后来辗转多地,找到如下方法,试了,可以

 

Java代码  收藏代码
  1. con.setRequestProperty("SOAPAction", "相应的soapaction");、  
Java代码  收藏代码
  1. 源代码如下:  
Java代码  收藏代码
  1. /** 
  2.      * 以post的方式模拟HTTP 
  3.      * @param urlStr 
  4.      * @param paras 
  5.      * @return 
  6.      */  
  7.     public String httpPost(String urlStr, String paras) {  
  8.         byte[] data = paras.getBytes();  
  9.         URL url = null;  
  10.         HttpURLConnection con = null;  
  11.         InputStream input = null;  
  12.         String response = null;  
  13.         try {  
  14.             url = new URL(urlStr);  
  15.             con = (HttpURLConnection)url.openConnection();  
  16.             con.setConnectTimeout(1000 * 6);  
  17.             //Server returned HTTP response code: 500 for URL:错误解决方案  
  18.             con.setRequestProperty("SOAPAction", "http:///IInsureBill/Submit");  
  19.               
  20.             con.setDoInput(true);  
  21.             con.setDoOutput(true);  
  22.             //如果为 true,则只要有条件就允许协议使用缓存  
  23.             con.setUseCaches(false);  
  24.             con.setRequestMethod("POST");  
  25.             con.setRequestProperty("Connection", "Keep-Alive");  
  26.             con.setRequestProperty("Charset", "UTF-8");  
  27.             con.setRequestProperty("Content-Length", String.valueOf(data.length));  
  28.             con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");  
  29.             con.connect();  
  30.             DataOutputStream outputStream = new DataOutputStream(con.getOutputStream());  
  31.             outputStream.write(data);  
  32.             outputStream.flush();  
  33.             outputStream.close();  
  34.             int responseCode = con.getResponseCode();  
  35.             if(responseCode == 200) {  
  36.                 input = con.getInputStream();  
  37.                 response = getResponse(input);  
  38.             }else {  
  39.                 input = con.getInputStream();  
  40.                 response = getResponse(input);  
  41.                 System.out.println(response);  
  42.                 response = "返回码为:"+responseCode;  
  43.             }  
  44.         }catch(Exception e) {  
  45.             e.printStackTrace();  
  46.         }finally {  
  47.             con.disconnect();  
  48.         }  
  49.         return response;  
  50.     }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多