如何着手调试 HttpURLConnection?
How to go about debugging HttpURLConnection?
我正在尝试将 java 应用程序连接到 GuildWars2 的外部 api。
我要测试的 link 是:
http://api.guildwars2.com/v2/commerce/listings
在浏览器中导航到该页面时返回一个 int ID 列表。
作为一种学习实践,我试图在 运行 我的 java 应用程序时获取该 ID 列表。
我使用以下代码(希望它的格式正确,目前在我的 phone 上,试图远程编程到我的桌面):
public class GuildWarsAPI
{
public static void main(String[] args)
{
GuildWarsAPI api = new GuildWarsAPI();
api.getAPIResponse("http://api.guildwars2.com/v2/commerce/listings");
}
public void getAPIResponse(String URLString)
{
URL url = null;
try {
url = new URL(URLString);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (connection != null)
{
System.out.println("connection success");
connection.setRequestProperty("Accept", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
try {
/*BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder input = new StringBuilder();
String nextLine = null;
while ((nextLine = in.readLine()) != null)
{
System.out.println("adding output");
input.append(nextLine);
}*/
InputStream in = new BufferedInputStream(connection.getInputStream());
int b = 0;
while ((b = in.read()) != -1)
{
System.out.println("byte:" + b);
}
System.out.println("done");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
connection.disconnect();
System.out.println("closed");
}
}
}
}
在 运行 我的 class 上,它立即打印出 connection success
、done
、closed
。它绝对不是在等待超时,我一直在尝试使用它、请求 header 和 DoInput/DoOutput。我逐步通过它,它看起来好像连接了,只是没有收到任何返回的信息字节。 (不进入 while
循环)
所以,虽然我的最终问题是:如何像我期望的那样取回 id?但我的另一个问题是:我怎样才能弄清楚如何像我期望的那样取回其他 id?
您的代码正在获取响应代码 302 Found
。它应该跟随 Location:
header 到新位置,因为默认情况下 followRedirects
为真,但事实并非如此。然而,服务器返回 Location:
header of https://api.guildwars2.com/v2/commerce/listings
。我不知道为什么 HttpURLConnection
没有遵循那个,但简单的解决方法是在原始 URL.
中使用 https:
您正在设置 doOutput(true)
但您没有发送任何输出。
您的代码结构不佳。依赖于先前 try
块中代码成功的代码应该在同一个 try
块中。我会有方法 throw MalformedURLException
和 IOException
并且根本没有任何内部 try/catch
块。
根据我的经验,与 HttpUrlConnection
较量麻烦得不偿失。
调试困难,使用困难,对复杂的http操作支持很少。
有很多更好的选择。
我的默认选择是 Apache HttpConponents Client (http://hc.apache.org/)。它不一定比所有其他选项更好,但它有很好的文档记录和广泛使用。
我正在尝试将 java 应用程序连接到 GuildWars2 的外部 api。
我要测试的 link 是: http://api.guildwars2.com/v2/commerce/listings
在浏览器中导航到该页面时返回一个 int ID 列表。
作为一种学习实践,我试图在 运行 我的 java 应用程序时获取该 ID 列表。
我使用以下代码(希望它的格式正确,目前在我的 phone 上,试图远程编程到我的桌面):
public class GuildWarsAPI
{
public static void main(String[] args)
{
GuildWarsAPI api = new GuildWarsAPI();
api.getAPIResponse("http://api.guildwars2.com/v2/commerce/listings");
}
public void getAPIResponse(String URLString)
{
URL url = null;
try {
url = new URL(URLString);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (connection != null)
{
System.out.println("connection success");
connection.setRequestProperty("Accept", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
try {
/*BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder input = new StringBuilder();
String nextLine = null;
while ((nextLine = in.readLine()) != null)
{
System.out.println("adding output");
input.append(nextLine);
}*/
InputStream in = new BufferedInputStream(connection.getInputStream());
int b = 0;
while ((b = in.read()) != -1)
{
System.out.println("byte:" + b);
}
System.out.println("done");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
connection.disconnect();
System.out.println("closed");
}
}
}
}
在 运行 我的 class 上,它立即打印出 connection success
、done
、closed
。它绝对不是在等待超时,我一直在尝试使用它、请求 header 和 DoInput/DoOutput。我逐步通过它,它看起来好像连接了,只是没有收到任何返回的信息字节。 (不进入 while
循环)
所以,虽然我的最终问题是:如何像我期望的那样取回 id?但我的另一个问题是:我怎样才能弄清楚如何像我期望的那样取回其他 id?
您的代码正在获取响应代码
302 Found
。它应该跟随Location:
header 到新位置,因为默认情况下followRedirects
为真,但事实并非如此。然而,服务器返回Location:
header ofhttps://api.guildwars2.com/v2/commerce/listings
。我不知道为什么HttpURLConnection
没有遵循那个,但简单的解决方法是在原始 URL. 中使用 您正在设置
doOutput(true)
但您没有发送任何输出。您的代码结构不佳。依赖于先前
try
块中代码成功的代码应该在同一个try
块中。我会有方法 throwMalformedURLException
和IOException
并且根本没有任何内部try/catch
块。
https:
根据我的经验,与 HttpUrlConnection
较量麻烦得不偿失。
调试困难,使用困难,对复杂的http操作支持很少。
有很多更好的选择。
我的默认选择是 Apache HttpConponents Client (http://hc.apache.org/)。它不一定比所有其他选项更好,但它有很好的文档记录和广泛使用。