开发者社区> 问答> 正文

post请求soap服务,Java代码报错?报错

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.hisun.webservice.WebServiceCsvFile.main(WebServiceCsvFile.java:36)


代码如下:

public class WebServiceCsvFile {  
  
    public static void main(String[] args) throws Exception {  
        String urlString = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";  
        String xmlFile = "111";// 要发送的soap格式文件  
         String soapActionString = "http://WebXml.com.cn/getWeatherbyCityName";//Soap 1.1中使用  
        URL url = new URL(urlString);  
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
        File fileToSend = new File(xmlFile);  
        byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组  
        new FileInputStream(xmlFile).read(buf);  
//      httpConn.setRequestProperty("Content-Length",  
//              String.valueOf(buf.length));//这句话可以不用写,即使是随便写  
        //根据我的测试,过去的请求头中的Content-Length长度也是正确的,也就是说它会自动进行计算  
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");  
         httpConn.setRequestProperty("soapActionString",soapActionString);//Soap  
        httpConn.setRequestMethod("POST");  
        httpConn.setDoOutput(true);  
        httpConn.setDoInput(true);  
        OutputStream out = httpConn.getOutputStream();  
        out.write(buf);  
        out.close();  
        InputStreamReader is = new InputStreamReader(httpConn.getInputStream(),"utf-8");  
        BufferedReader in = new BufferedReader(is);  
        String inputLine;  
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(  
                new FileOutputStream("result.xml")));// 将结果存放的位置  
        while ((inputLine = in.readLine()) != null) {  
            System.out.println(inputLine);  
            bw.write(inputLine);  
            bw.newLine();  
        }  
        bw.close();  
        in.close();  
        httpConn.disconnect();  
    }  
}  

展开
收起
爱吃鱼的程序员 2020-06-09 10:40:45 581 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    看异常信息是webservice端处理请求时发生异常了,如果服务端是自已的就调试一下,如果不是就用soapui跑一下看看是否正常,然后再对比一下你post的数据看看是否一致,想法调整,我对java不熟,只能说个大概的思路是这样的。wsdl是不是有windows登录验证

    2020-06-09 10:41:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载