简体   繁体   English

URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400

[英]URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL

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. 我试图从桌面应用程序连接到一个URL,我得到我的问题的标题中指示的错误,但当我尝试从servlet连接到相同的URL时,一切正常。 When I load the URL from browser, all works fine. 当我从浏览器加载URL时,一切正常。 I am using the same code in the servlet. 我在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. 代码在库中,当它不起作用时,我将代码拉出到当前项目中的一个类,但它没有用。

The URL https://graph.facebook.com/me . 网址https://graph.facebook.com/me

The Code fragment. 代码片段。

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();
}   

I'm a little bit confused here, is there something that is present is a servlet that is not a normal desktop app? 我在这里有点困惑,是否存在一些不是普通桌面应用程序的servlet?

Thanks. 谢谢。

FULL STACK TRACE 完全堆积的痕迹

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)

EDIT: You need to find the exact error message that facebook is sending in the response You can modify your code to get the message from the error stream like so: 编辑:您需要找到Facebook在响应中发送的确切错误消息您可以修改您的代码以从错误流中获取消息,如下所示:

HttpURLConnection httpConn = (HttpURLConnection)connection;
InputStream is;
if (httpConn.getResponseCode() >= 400) {
    is = httpConn.getErrorStream();
} else {
    is = httpConn.getInputStream();
}

Take a look at how you are passing the user context Here's some information that could help you out: 看看你如何传递用户上下文这里有一些可以帮助你的信息:

Look at the error message behind the 400 response code: 查看400响应代码背后的错误消息:

"Facebook Platform" "invalid_request" "An active access token must be used to query information about the current user* “Facebook平台”“invalid_request” “必须使用活动访问令牌来查询有关当前用户的信息*

You'll find the solution here 你会在这里找到解决方案

HTTP/1.1 400 Bad Request
...
WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "An active access token must be used to query information about the current user."
...

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. 数据必须只包含名称和描述,但我传递的不仅仅是名称和描述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpURLConnection错误-java.io.IOException:服务器返回的HTTP响应代码:URL的400 - HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL 服务器返回 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400 - server returns java.io.IOException: Server returned HTTP response code: 400 for URL 服务器返回URL的HTTP响应代码:400:java.io.IOException - Server returned HTTP response code: 400 for URL : java.io.IOException java.io.IOException:服务器返回HTTP响应代码:400表示URL - java.io.IOException: Server returned HTTP response code: 400 for URL 获取java.io.IOException:服务器返回的HTTP响应代码:URL为400,但可以在浏览器中正常访问 - Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser java.io.IOException:服务器为URL返回HTTP响应代码:400 - java.io.IOException: Server returned HTTP response code: 400 for URL: java.io.IOException:服务器返回的HTTP响应代码:URL的400 - java.io.IOException: Server returned HTTP response code: 400 for URL Java Http(s)URLConnection java.io.IOException:服务器返回 HTTP 响应代码:403 - Java Http(s)URLConnection java.io.IOException: Server returned HTTP response code: 403 错误打开连接 java.io.IOException:服务器返回 HTTP 响应代码:URL 501 - Error opening connection java.io.IOException: Server returned HTTP response code: 501 for URL 错误:java.io.IOException:服务器返回 HTTP 响应代码:URL 403 - Error : java.io.IOException: Server returned HTTP response code: 403 for URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM