URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400 [英] URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL
问题描述
我正在尝试连接到桌面应用程序的URL,我收到了问题标题中指出的错误,但是当我尝试从servlet连接到同一个URL时,一切正常。当我从浏览器加载URL时,一切正常。我在servlet中使用相同的代码。代码在库中,当它不起作用时,我将代码拉出到当前项目中的一个类,但它没有用。
I am trying to connect to a URL from a desktop app, and I get the error indicated in the Title of my question, but when I tried to connect to the same URL from servlet, all works fine. When I load the URL from browser, all works fine. I am using the same code in the servlet. The code was in a library, when it didn't work, I pulled the code out to a class in the current project, yet it didn't work.
网址 https://graph.facebook.com/me 。
代码片段。
public static String post(String urlSpec, String data) throws Exception {
URL url = new URL(urlSpec);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(data);
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder builder = new StringBuilder();
while((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
}
我在这里有点困惑,有什么东西存在一个不是普通桌面应用程序的servlet?
I'm a little bit confused here, is there something that is present is a servlet that is not a normal desktop app?
谢谢。
完全堆栈跟踪
Feb 8, 2011 9:54:14 AM com.trinisoftinc.jiraffe.objects.FacebookAlbum create
SEVERE: null
java.io.IOException: Server returned HTTP response code: 400 for URL: https://graph.facebook.com/me
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at com.jiraffe.helpers.Util.post(Util.java:49)
at com.trinisoftinc.jiraffe.objects.FacebookAlbum.create(FacebookAlbum.java:211)
at com.trinisoftinc.jiraffe.objects.FacebookAlbum.main(FacebookAlbum.java:261)
推荐答案
我终于找到了问题。当然这是我的代码。我没有发布的代码的一部分是数据的价值。数据必须只包含名称和描述,但我传递的不仅仅是名称和描述。
I finally found the problem. Of course it's my code. One part of the code I didn't post is the value of data. data must contain only name and description but I am passing more than name and description.
这篇关于URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!