HTTPURLConnection - 空响应

时间:2014-03-28 02:38:23

标签: java ajax xml

这可能是一个常见的问题,但我无法弄清楚。

我有这个URL,如果我将它插入浏览器,它会返回预期的票证明细,所以我知道它有效:

https://www.myconnectwise.net/v4_6_release/services/system_io/integration_io/processClientAction.rails?actionString=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-16%22%3F%3E%0A%3CGetTicketAction%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchemainstance%22%20xmlns%3Axsd%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%3E%0A%3CCompanyName%3Exxxxxxx%3C%2FCompanyName%3E%3CIntegrationLoginId%3Exxxxxx%3C%2FIntegrationLoginId%3E%3CIntegrationPassword%3Exxxxxx%3C%2FIntegrationPassword%3E%0A%3CSrServiceRecid%3E1921%3C%2FSrServiceRecid%3E%3C%2FGetTicketAction%3E

对于更易读的版本,就是这个URL:

https://www.myconnectwise.net/v4_6_release/services/system_io/integration_io/processClientAction.rails

其次是:

?actionString=

后面跟着这个xml:

<?xml version="1.0" encoding="utf-16"?>
<GetTicketAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CompanyName>xxxx</CompanyName><IntegrationLoginId>xxxx</IntegrationLoginId><IntegrationPassword>xxxx</IntegrationPassword>
<SrServiceRecid>1921</SrServiceRecid></GetTicketAction>

我在将此代码放入代码并使其工作时遇到了问题。

我收到此错误:

????????????????????????????????????????????????????????????????????????????????????????????    ????????????????????????????????????????????        o??????????????????????????????????????????????????????????????????????????????????????????    ???????????????????????????????????????????????????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????o???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
com.google.gwt.xml.client.impl.DOMParseException: Failed to parse: error on line 1 at column 1: Document is empty

at com.google.gwt.xml.client.impl.XMLParserImplSafari.throwDOMParseException(XMLParserImplSafari.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.xml.client.impl.XMLParserImplSafari.parseImpl(XMLParserImplSafari.java)
at com.google.gwt.xml.client.impl.XMLParserImpl.parse(XMLParserImpl.java:278)
at com.google.gwt.xml.client.XMLParser.parse(XMLParser.java:47)
at com.google.gwt.HelpDeskTest.client.HelpDeskTest.processXML(HelpDeskTest.java:232)
at com.google.gwt.HelpDeskTest.client.HelpDeskTest$2$1.onSuccess(HelpDeskTest.java:199)
at com.google.gwt.HelpDeskTest.client.HelpDeskTest$2$1.onSuccess(HelpDeskTest.java:1)
at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:232)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Thread.java:695)

在这一行:

System.out.println(line);

这是所有代码。如您所见,我已尝试将serverParameters设置为URL编码字符串以及xml字符串:

package com.google.gwt.HelpDeskTest.server;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.gwt.HelpDeskTest.client.HelpDeskTestService;
import com.google.gwt.HelpDeskTest.shared.HelpDeskTestException;


@SuppressWarnings("serial")
public class HelpDeskTestImpl extends RemoteServiceServlet implements
    HelpDeskTestService {

@Override
public String postToRemoteServer(String serviceUrl)
        throws HelpDeskTestException {

    try {

        final String serverPath = "https://www.myconnectwise.net/v4_6_release/services/system_io/integration_io/processClientAction.rails";

        System.out.println(serverPath);


        //DO I need to URL encode this???
        //I’ve tried this:
        //final String serverParameters= "%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-16%22%3F%3E%0A%3CGetTicketAction%20xmlns%3Axsi%3D%22http%3A%2F%2F" +
        //"www.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Axsd%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%3E%0A%3CCompanyName" + 
        //"%3Exxxxx%3C%2FCompanyName%3E%3CIntegrationLoginId%3Exxxxx%3C%2FIntegrationLoginId%3E%3CIntegrationPassword%3Exxxxx%3C%2F" + 
        //"IntegrationPassword%3E%0A%3CSrServiceRecid%3E1921%3C%2FSrServiceRecid%3E%3C%2FGetTicketAction%3E";

        final String serverParameters="<?xml version=%221.0%22 encoding=%22utf-16%22?><GetTicketAction xmlns:xsi=%22http://www.w3.org/2001/XMLSchema-instance%22 xmlns:xsd=%22http://www.w3.org/2001/XMLSchema%22><CompanyName>xxxx</CompanyName><IntegrationLoginId>xxxx</IntegrationLoginId><IntegrationPassword>xxxx</IntegrationPassword><SrServiceRecid>1921</SrServiceRecid></GetTicketAction>";

        System.out.println(serverParameters);


        //Open HttpURLConnection:           

        URL url = new URL(serverPath); 
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();   

        connection.setConnectTimeout(10000); //added this to see if I can address the timeout issue.
        connection.setReadTimeout(10000);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("charset", "utf-16");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(serverParameters.getBytes().length));
        connection.setUseCaches (false);

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
        wr.writeBytes(serverParameters);
        wr.flush();
        wr.close();

        //process response - need to get xml response back.
        InputStream stream = connection.getInputStream();

        //put output stream into a string
        BufferedReader br = new BufferedReader(new InputStreamReader(stream,"utf-16"));
        String result = "";
        String line;
        while ((line = br.readLine()) != null) {
        System.out.println(line);
           result+= line;
        }

        br.close();
        connection.disconnect();

        System.out.println(result);

       return result;

    }  catch (final Exception e) {
        System.out.println(e.getMessage());
        throw new HelpDeskTestException();
        //handle timeout error

    }
}

}

1 个答案:

答案 0 :(得分:1)

我得到了服务器返回的HTTP响应代码:500为URL:myconnectwise.net/v4_6_release/services/system_io / ...;执行代码后。 所以检查服务器(500 - 内部服务器错误)。 并在将结果解析为XML之前分析响应代码。