gpt4 book ai didi

java - HTTPURLConnection 返回乱码

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

我正在修改一些工作代码以使用不同提供商的 API(我们正在切换帮助台提供商)。

我试图查看返回的 xml,看看我是否走在正确的轨道上,但我看到返回的都是乱码。我看过this问题,但无法弄清楚这些答案如何适用于我的情况。

如果我没记错的话,当我使用其他 API 时,我能够在控制台中读取返回的 xml:

while ((line = br.readLine()) != null) {
System.out.println(line);
}

我的问题是:有没有办法可以以不同的方式读取流,以便我可以读取返回的 xml,或者我还有其他问题吗?

我对此还很陌生,所以任何想法都会受到赞赏。更多详细信息如下。

代码:

package com.google.gwt.HelpDeskTest.server;

import java.io.BufferedReader;
import java.io.DataOutputStream;
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);


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>xxxxxx</CompanyName><IntegrationLoginId>xxxxxxx</IntegrationLoginId><IntegrationPassword>xxxxxx</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);
//connection.setChunkedStreamingMode(0);

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

//process response - need to get xml response back.
//this was the working line of code:
InputStream stream = connection.getInputStream();

//put output stream into a string
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
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

}
}
}

这是我尝试发送的 xml。我已经通过公司的 API 测试器对其进行了测试,知道它可以工作,并通过发送 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>xxxxxx</CompanyName>
<IntegrationLoginId>xxxxxx</IntegrationLoginId>
<IntegrationPassword>xxxxx</IntegrationPassword>
<SrServiceRecid>1921</SrServiceRecid>
</GetTicketAction>

最佳答案

当您发送数据时,您指定 utf-16 作为编码。

但是当您读取响应时,您没有指定编码,因此使用默认的平台编码。

所以交换这一行:

BufferedReader br = new BufferedReader(new InputStreamReader(stream));

这样(假设响应也以 utf-16 编码):

BufferedReader br = new BufferedReader(new InputStreamReader(stream,"utf-16"));

您实际上应该检查响应 header 以了解使用了哪种编码。

关于java - HTTPURLConnection 返回乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22696950/

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