关于在java程序里调用webservice报500返回码的问题

J2EE 码拜 10年前 (2015-04-19) 990次浏览 0个评论
 

我现在写了个程序,是调用webservice的,执行后我打印返回码是500  错误信息是

java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.45.7.18:8071/ocswebservices/services/WebServices?wsdl
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1133)
	at com.ztesoft.zsmart.bss.sett.appstore.bll.ProtoSoapHTTPBase.write(ProtoSoapHTTPBase.java:167)
	at com.ztesoft.zsmart.bss.sett.appstore.bll.ProtoSoapHTTPBase.main(ProtoSoapHTTPBase.java:284)

我谷歌了下,所有的回答都是java服务器做了限制,需要加上

con.addRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE5.5;Windows NT; DigExt)");

 但是我加上了还是没用  URL写http://10.45.7.18:8071/ocswebservices/services/WebServices?wsdl或者http://10.45.7.18:8071/ocswebservices/services/WebServices都不行。
错误是

InputStream input = con.getInputStream();

引起的
如果url写成http://10.45.7.18:8071/ocswebservices,会有正常的返回(普通页面,不是webservice)
这个怎么办?

附上java代码,其实可以不用看。。

package com.ztesoft.zsmart.bss.sett.appstore.bll;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import org.apache.axis.client.Call;
import org.apache.log4j.Logger;


import com.ztesoft.zsmart.core.utils.StringUtil;

/**
 * <Description> <br>
 * 
 * @author ProvTeam<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2012-2-5 <br>
 * @since V7.3<br>
 * @see <br>
 */
public class ProtoSoapHTTPBase {
    /**
     * 读缓存的大小
     */
    public final int readBufferSize = 1024 * 10;

    /**
     * 日志对象
     */
    private static Logger logger = Logger.getLogger(ProtoSoapHTTPBase.class);

    /**
     * 主机地址
     */
    protected String host = null;

    /**
     * 端口
     */
    protected String port = null;

    /**
     * 路径
     */
    protected String path = null;

    /**
     * 是否是HTTPS协议
     */
    protected boolean isHttps = false;

    /**
     * http协议的返回
     */
    byte[] httpResp = null;

    /**
     * 是否可读标记
     */
    protected boolean toRead = false;

    /**
     * URL
     */
    protected URL url = null;

    /**
     * http链接客户端
     */
    protected HttpURLConnection httpClient = null;

    /**
     * https链接客户端
     */
    protected HttpURLConnection httpsClient = null;

    // private boolean terminated = false;

    /**
     * webservice 服务url
     */
    protected String serverAddress = null;

    /**
     * webservice 服务SOAPAction
     */
    protected String soapAction = null;

    /**
     * 调用webservice超时
     */
    protected int timeout = 5000;

    /**
     * 底层调用
     */
    protected Call callHandler = null;

    /**
     * 回复消息
     */
    byte[] soapResp = null;

    /**
     * 编码
     */
    private String encoding = "UTF-8";

    /***************************************************************************
     * 写信息
     * 
     * @param data byte[]
     * @return int
     */
    public int write(byte[] data) {
        // TODO Auto-generated method stub

        if (data == null) {
            return -2;
        }
        try {

            logger.debug(">>>>>>>>>send to the Http Server.....");
            String req = new String(data);
            logger.info("HTTP request:" + req);
            logger.info("url:" + this.url);

            if ("Default".equals(encoding)) {

            }
            else {
                data = req.getBytes(encoding);
            }

            System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(5 * 1000));
            System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(this.timeout));

            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            // Assert.isNotNull(con, "Http Connection is Null");
            if (con == null) {
                return -2;
            }
            con.setRequestMethod("POST");
            con.setUseCaches(false);
            con.setDoOutput(true);
            con.setDoInput(true);
            con.addRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE5.5;Windows NT; DigExt)");
            con.setRequestProperty("Content-type", "application/soap+xml; charset=utf-8");
            con.setAllowUserInteraction(true);
            con.connect();

            OutputStream output = con.getOutputStream();
            // Assert.isNotNull(output, "Http outputstream is Null");

            output.write(data);
            output.flush();
            output.close();
            // Assert.isNotNull(con, "con is Null");
            InputStream input = con.getInputStream();
            // Assert.isNotNull(input, "Http inputstream is Null");

            try {
                this.soapResp = readData(input);

                logger.info("HTTP response:" + new String(soapResp, this.encoding));
            }
            finally {
                if (con != null) {
                    con.disconnect();
                    con = null;
                }
            }
        }
        catch (ProtocolException ex) {
            logger.error("The Http connection UNSupport the Post method.", ex);
            ex.printStackTrace();
            return -1;
        }
        catch (IOException ex) {
            logger.error("The Http connection Stream error,", ex);
            ex.printStackTrace();
            return -2;
        }
        catch (Exception ex) {
            logger.error("The Http other error,", ex);
            ex.printStackTrace();
            return -3;
        }

        return data.length;
    }

    /***************************************************************************
     * 读信息
     * 
     * @param input InputStream
     * @return byte[]
     */
    protected byte[] readData(InputStream input) {
        byte[] res = null;
        try {
            if (input != null) {

                int len = 0;
                int length = readBufferSize;
                int offset = 0;
                byte[] inputBuffer = new byte[length];

                while ((len = input.read(inputBuffer, offset, length - offset)) >= 0) {
                    logger.info("read length: " + len);
                    offset += len;

                }
                if (len < 0) {
                    logger.warn("The http has closed.....");
                }
                if (offset > 0) {
                    logger.info("read total length: " + offset);
                    res = new byte[offset];
                    System.arraycopy(inputBuffer, 0, res, 0, offset);

                    if (input != null) {
                        input.close();
                    }

                }
            }
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return res;
    }

    public static void main(String[] args) throws MalformedURLException {
        StringBuffer sb = new StringBuffer();

    	sb.append("<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ocs="http://ocs.ztesoft.com">");
    	sb.append("     <soapenv:Header>");
    	sb.append("        <ns1:AuthHeader soapenv:mustUnderstand="0" xmlns:ns1="http://ocs.ztesoft.com">");
    	sb.append("        <ns1:Username>zsmart1</ns1:Username>");
    	sb.append("        <ns1:Password>zsmart1</ns1:Password>");
    	sb.append("        </ns1:AuthHeader>");
    	sb.append("    </soapenv:Header>   ");
    	sb.append("");
    	sb.append("   <soapenv:Body>");
    	sb.append("      <ocs:doService>");
    	sb.append("         <ocs:in0><![CDATA[");
    	sb.append("<?xml version="1.0" encoding="GB2312"?>");
    	sb.append("<zsmart>");
    	sb.append("		<Data>");
    	sb.append("			<header>");
    	sb.append("				<ACTION_ID>PayForPrm</ACTION_ID>");
    	sb.append("				<REQUEST_ID>10200700021</REQUEST_ID>");
    	sb.append("			</header>");
    	sb.append("			<body>				");
    	sb.append("				<PAY_DIRECTION>P</PAY_DIRECTION>				");
    	sb.append("				<STAFF_ID>1</STAFF_ID>");
    	sb.append("				<BILL_INST_ID>118</BILL_INST_ID>");
    	sb.append("				<PARTNER_ID>644</PARTNER_ID>");
    	sb.append("				<AMOUNT>30000</AMOUNT>");
    	sb.append("				<CHANGE>3000</CHANGE>");
    	sb.append("				<PAYMENT_METHOD>C</PAYMENT_METHOD>									");
    	sb.append("			</body>");
    	sb.append("		</Data>");
    	sb.append("</zsmart>");
    	sb.append("");
    	sb.append("");
    	sb.append("]]></ocs:in0>");
    	sb.append("      </ocs:doService>");
    	sb.append("   </soapenv:Body>");
    	sb.append("</soapenv:Envelope>");
    
    	ProtoSoapHTTPBase p = new ProtoSoapHTTPBase();
    	p.url = new URL("http://10.45.7.18:8071/ocswebservices/services/WebServices?wsdl");
    	int i = p.write(sb.toString().getBytes());
    	System.out.println(i);
	}
}

关于在java程序里调用webservice报500返回码的问题
送一个谷歌好用的地址 http://84.15.64.35/
关于在java程序里调用webservice报500返回码的问题
40分
看不懂,不过帮顶…
关于在java程序里调用webservice报500返回码的问题
http://10.45.7.18:8071/ocswebservices/services/WebServices?wsdl可以在浏览器直接访问

通过soapUI发送报文可以查询(http://10.45.7.18:8071/ocswebservices/services/WebServices)。

关于在java程序里调用webservice报500返回码的问题
说简单点就是java给webservice发送完整的xml格式的报文 然后就收返回的数据
关于在java程序里调用webservice报500返回码的问题
10分
服务器端有没有验证,如有加上验证,如下

/**

public class MyAuthenticator extends Authenticator {
    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
 
        String username = "";
        String password = "";

        ReadProperties rnp = ReadProperties.getInstance();

		username = StrUtil.isBlank(username) ? "admin" : username;
		password = StrUtil.isBlank(password) ? "admin123" : password;

		// Return the information
        return new PasswordAuthentication(username, password.toCharArray());
    }

}
关于在java程序里调用webservice报500返回码的问题
5分
你浏览器里访问 webservice 可以吗?
关于在java程序里调用webservice报500返回码的问题
5分
500好像是服务器错误吧
关于在java程序里调用webservice报500返回码的问题
引用 6 楼 rui888 的回复:

你浏览器里访问 webservice 可以吗?

浏览器输入http://10.45.7.18:8071/ocswebservices/services/WebServices?wsdl是可以的
soapui发送报文也是可以的。

关于在java程序里调用webservice报500返回码的问题
这种提示的错误是

java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.45.7.18:8071/ocswebservices/services/WebServices?wsdl
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1133)
	at com.ztesoft.zsmart.bss.sett.appstore.bll.a._callHttp(a.java:85)
	at com.ztesoft.zsmart.bss.sett.appstore.bll.a.main(a.java:104)

package com.ztesoft.zsmart.bss.sett.appstore.bll;

import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.lang.Integer;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.rpc.ParameterMode;

public class a {

    public static String _callHttp(String callurl) throws Exception {

        StringBuffer sb = new StringBuffer();

        sb.append("<soapenv:Envelope    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:ocs="http://ocs.ztesoft.com">\r\n");
        sb.append("                    <soapenv:Header>\r\n");
        sb.append("                                <ns1:AuthHeader    soapenv:mustUnderstand="0"    xmlns:ns1="http://ocs.ztesoft.com">\r\n");
        sb.append("                                <ns1:Username>zsmart1</ns1:Username>\r\n");
        sb.append("                                <ns1:Password>zsmart1</ns1:Password>\r\n");
        sb.append("                                </ns1:AuthHeader>\r\n");
        sb.append("                </soapenv:Header>            \r\n");
        sb.append("\r\n");
        sb.append("            <soapenv:Body>\r\n");
        sb.append("                        <ocs:doService>\r\n");
        sb.append("                                    <ocs:in0><![CDATA[\r\n");
        sb.append("<?xml    version="1.0"    encoding="UTF-8"?>\r\n");
        sb.append("<zsmart>\r\n");
        sb.append("                                <Data>\r\n");
        sb.append("                                                <header>\r\n");
        sb.append("                                                                <ACTION_ID>PayForPrm</ACTION_ID>\r\n");
        sb.append("                                                                <REQUEST_ID>10200700021</REQUEST_ID>\r\n");
        sb.append("                                                </header>\r\n");
        sb.append("                                                <body>                                                                \r\n");
        sb.append("                                                                <PAY_DIRECTION>P</PAY_DIRECTION>                                                                \r\n");
        sb.append("                                                                <STAFF_ID>1</STAFF_ID>\r\n");
        sb.append("                                                                <BILL_INST_ID>118</BILL_INST_ID>\r\n");
        sb.append("                                                                <PARTNER_ID>644</PARTNER_ID>\r\n");
        sb.append("                                                                <AMOUNT>30000</AMOUNT>\r\n");
        sb.append("                                                                <CHANGE>3000</CHANGE>\r\n");
        sb.append("                                                                <PAYMENT_METHOD>C</PAYMENT_METHOD>                                                                                                                                                \r\n");
        sb.append("                                                </body>\r\n");
        sb.append("                                </Data>\r\n");
        sb.append("</zsmart>\r\n");
        sb.append("\r\n");
        sb.append("\r\n");
        sb.append("]]></ocs:in0>\r\n");
        sb.append("                        </ocs:doService>\r\n");
        sb.append("            </soapenv:Body>\r\n");
        sb.append("</soapenv:Envelope>\r\n");

        String xml = sb.toString();
        StringReader reader = null;
        String result = "";
        URL u0 = new URL(callurl);
        HttpURLConnection conn = (HttpURLConnection) u0.openConnection();
        conn.setRequestMethod("POST"); // 采用post方法发送
        byte[] contentbyte = xml.getBytes(); // 内容转变成byte数组
        // 必须加"Content-Type","text/plain",然后接收方的页面写<%@ page
        // contentType="text    ml;    charset=gb2312" %>
        conn.setRequestProperty("Content-Type", "text/plain");
        conn.setRequestProperty("Content-Length", "" + contentbyte.length);
        conn.setRequestProperty("Content-Language", "en-US");
        conn.addRequestProperty("User-Agent",
                "Mozilla/4.0(compatible;MSIE5.5;Windows    NT;    DigExt)");
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream out = conn.getOutputStream();
        out.write(contentbyte);
        out.flush();
        out.close();

        InputStream in = conn.getInputStream(); // 取返回值
        StringBuffer buffer = new StringBuffer();
        int i = 0;
        while (i != -1) {
            i = in.read();
            if (i != -1) {
                buffer.append((char) i);
            }
        }
        in.close();
        result = new String(buffer.toString().getBytes("iso-8859-1"), "UTF-8");
        return result;

    }

    public static void main(String[] args) {
        try {
            System.out
                    .println(a
                            ._callHttp("http://10.45.7.18:8071/ocswebservices/services/WebServices?wsdl"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
关于在java程序里调用webservice报500返回码的问题
35分
看不出来。 我们没法测试啊。
关于在java程序里调用webservice报500返回码的问题
5分
conn.setRequestMethod(“POST”); 改为conn.setRequestMethod(“soap”)

webservice不能发post请求吧


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于在java程序里调用webservice报500返回码的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!