gpt4 book ai didi

java - HTTPURLConnection - 空响应

转载 作者:行者123 更新时间:2023-12-01 13:12:57 25 4
gpt4 key购买 nike

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

我有这个 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

}
}

}

最佳答案

我收到“服务器返回 HTTP 响应代码:500,URL:myconnectwise.net/v4_6_release/services/system_io/…”;执行代码后。因此请检查服务器(500 - 内部服务器错误)。并在将结果解析为 XML 之前分析响应代码。

关于java - HTTPURLConnection - 空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22703192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com