Java.io.IOException: HTTP请求失败,HTTP状态码:500(KSOAP2)

4

我正在使用KSOAP2,在向服务器发送请求时,httpTransport.call(SOAP_ACTION, envelope)所在行出现了java.io.IOException: HTTP request failed, HTTP status: 500的错误提示,但服务器是正常工作的,我已经用SoapUI进行过检查。这可能是什么问题呢?

public class SOAPClient
{
    private static final int TIMEOUT_SOCKET = 180000;

    public static SoapObject get(Context context, String methodName, ArrayList<Pair<String, ?>> args) throws Exception
    {
        final String URL = PrefHelper.getSOAPUrl(context);
        final String NAMESPACE = PrefHelper.getSOAPNamespace(context);
        final String SOAP_ACTION = methodName; //NAMESPACE + "#" + "mapx" + ":" + methodName;

        SoapObjectEve request = new SoapObjectEve(NAMESPACE, methodName);

        if (args != null) {
            for (Pair<String, ?> arg : args) {
                if (arg.first != null) {
                    request.addProperty(arg.first, arg.second);
                }
            }
        }

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        new MarshalBase64().register(envelope);
        envelope.setOutputSoapObject(request);

        envelope.implicitTypes = true;

        HttpTransportSE httpTransport = new HttpTransportSE(URL, TIMEOUT_SOCKET);
        httpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        try 
        {
            httpTransport.call(SOAP_ACTION, envelope);
            AppLog.e(httpTransport.requestDump+"requestDump");


        }
        catch (ConnectTimeoutException e) {
            AppLog.e(e.getMessage());
            throw new Exception(context.getString(R.string.node_unavailable));
        }
        catch (SocketTimeoutException e) {
            AppLog.e(e.getMessage());
            throw new Exception(context.getString(R.string.timeout));
        }
        catch (Exception e) {
            e.printStackTrace();
            AppLog.e(e.getMessage());

            AppLog.e(httpTransport.requestDump+"requestDump");
            throw new Exception(context.getString(R.string.warning_error_get_data) + e.getMessage() == null ? "" : " " + e.getMessage());
        }

        AppLog.i(httpTransport.requestDump+"requestDump");

        SoapObject soapObj = null;

        try {
            soapObj = (SoapObject) envelope.getResponse();
        }
        catch (Exception e) {

            String response = ((SoapPrimitive) envelope.getResponse()).toString();
            boolean res = Boolean.valueOf(response);

            soapObj = new SoapObject();
            soapObj.addProperty("response", res);
            soapObj.addProperty("msg", "");
            soapObj.addProperty("data", null);

        }
        AppLog.e(httpTransport.responseDump+"responseDump");
        return soapObj;
    }
}

"500" 表示发生了内部服务器错误。请检查您的服务器日志以查看出现了什么问题。 - K Neeraj Lal
@KNeerajLal 服务器错误日志中没有错误。 - Lucky_girl
你的请求转储看起来是什么样子的?它是否与wsdl指定的相同? - K Neeraj Lal
@KNeerajLal 我在 access.log 中看到了一个错误,但是在 Apache 的 error.log 中没有发现任何错误。 - Lucky_girl
@KNeerajLal 我已经尝试过了,但它没有显示在生成的日志XML中。 - Lucky_girl
显示剩余4条评论
1个回答

3
请确保您的NAMESPACEMETHODNAMEWSDLSOAP_ACTION正确无误。
试用以下代码,
private static final String NAMESPACE = "http://www.kltro.com";
private static final String METHODNAME = "getUser";
private static final String WSDL = "http://ap.kashkan.org:55555/tro/ws/kltro";
private static final String SOAP_ACTION = NAMESPACE + "#kltro:" + METHODNAME ;

private static String TAG = "soap";

public static String callWebservice() {
    String responseDump = "";
    try {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
        request.addProperty("login", login);
        request.addProperty("pass", password);
        request.addProperty("androidID", androidID);
        request.addProperty("ver", version);

        envelope.bodyOut = request;
        HttpTransportSE transport = new HttpTransportSE(WSDL);
        transport.debug = true;
        try {
            transport.call(SOAP_ACTION, envelope);
            String requestDump = transport.requestDump;
            responseDump = transport.responseDump;
            Log.e(TAG, requestDump);
            Log.e(TAG, responseDump);
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseDump;
}

同时确保在清单文件中获得了互联网权限。

<uses-permission android:name="android.permission.INTERNET" />

当运行这种类型的方法两次,比如用于许可证验证时,我发现第二次会出现错误。因此,如果你输入了一次无效的许可证,然后输入正确的许可证,我遇到了 IOException 500 状态。有什么想法是什么导致了这个问题吗? - whyoz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接