我正在修改一些工作代碼以使用其他提供者的API(我們正在切換幫助臺提供者)。HTTPURLConnection返回亂碼
我想看看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>
嘗試'connection.getContent()' – anttix