服务器返回 HTTP 响应代码:400

发布于 2025-01-07 00:51:43 字数 615 浏览 1 评论 0原文

我正在尝试从 URL 获取 InputStream。该 URL 可以从 Firefox 打开。它返回一个 json,我已经安装了一个用于在 Firefox 中查看 json 的插件,这样我就可以在那里查看它。

所以我尝试通过以下方式从 Java 获取它:

URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

但它在 urlConnection.getInputStream() 中抛出 IOException。

我也尝试过:

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = url.openStream();

但没有运气。

任何信息都是值得重视的。提前致谢。

I am trying to get an InputStream from a URL. The URL can be a opened from Firefox. It returns a json and I have installed an addon for viewing json in Firefox so I can view it there.

So I tried to get it from Java by:

URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

But it is throwing an IOException in urlConnection.getInputStream().

I also tried:

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = url.openStream();

But no luck.

Any information is appreciable. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清欢 2025-01-14 00:51:43

谢谢大家。这是一个奇怪的问题,但最后我解决了。

我请求的URL

http://api.themoviedb.org/2.1/Movie.search/en/json/api_key/a nightmare on elm street 

现在浏览器在内部用“%20”替换“榆树街上的噩梦”之间的空格并解析。这就是为什么被请求的服务器可以通过该请求进行响应。但是从Java中我没有用“%20”替换那个空格,所以它变成了错误请求, 来源

现在它正在发挥作用。

BufferedReader reader = new BufferedReader(new InputStreamReader(((HttpURLConnection) (new URL(urlString)).openConnection()).getInputStream(), Charset.forName("UTF-8")));

Thank you everybody. This is a weird problem but at last I solved it.

The URL I am requesting is

http://api.themoviedb.org/2.1/Movie.search/en/json/api_key/a nightmare on elm street 

Now browser replaces the spaces between "a nightmare on elm street" by "%20" internally and parses. That is why the requested server can response by that request. But From Java I didn't replaced that spaces by "%20", so it turns into Bad Request, source.

Now it is working.

BufferedReader reader = new BufferedReader(new InputStreamReader(((HttpURLConnection) (new URL(urlString)).openConnection()).getInputStream(), Charset.forName("UTF-8")));
梦年海沫深 2025-01-14 00:51:43

我遇到了类似的问题,我的网址是:

http://www.itmat.upenn.edu/assets/user-content/documents/ITMAT17. October 10 2017_.pdf

其中显然包含空格。

这些导致java.io.IOException服务器返回HTTP响应代码:400在以下代码中:

java.net.URL url = new URL(urlString);  
java.io.InputStream in = url.openStream();

如果你复制上面的url并粘贴到浏览器中,你会发现浏览器为空格添加了“%20” 。所以我用下面的代码手动完成,问题就解决了。

if(urlString.contains(" "))
    urlString = urlString.replace(" ", "%20");

完整的代码/答案应该是:

if(urlString.contains(" "))
    urlString = urlString.replace(" ", "%20");
java.net.URL url = new URL(urlString);  
java.io.InputStream in = url.openStream();

I had a similar issue and my url was:

http://www.itmat.upenn.edu/assets/user-content/documents/ITMAT17. October 10 2017_.pdf

which obviously contained spaces.

These caused java.io.IOException Server returned HTTP response code: 400 in the following code:

java.net.URL url = new URL(urlString);  
java.io.InputStream in = url.openStream();

If you copy the above url and paste in browser, you will realize that browser adds '%20' for the spaces. So I did it manually with the following code and the problem is solved.

if(urlString.contains(" "))
    urlString = urlString.replace(" ", "%20");

Complete code/answer should be:

if(urlString.contains(" "))
    urlString = urlString.replace(" ", "%20");
java.net.URL url = new URL(urlString);  
java.io.InputStream in = url.openStream();
以酷 2025-01-14 00:51:43

您是否正确设置连接?这里有一些代码说明了如何执行此操作。请注意,我在这里对异常处理很懒,这不是生产质量代码。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class URLFetcher {

    public static void main(String[] args) throws Exception {
        URL myURL = new URL("http://www.paulsanwald.com/");
        HttpURLConnection connection = (HttpURLConnection) myURL.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder results = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            results.append(line);
        }

        connection.disconnect();
        System.out.println(results.toString());
    }
}

are you setting up the connection correctly? here's some code that illustrates how to do this. Note that I am being lazy about exception handling here, this is not production quality code.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class URLFetcher {

    public static void main(String[] args) throws Exception {
        URL myURL = new URL("http://www.paulsanwald.com/");
        HttpURLConnection connection = (HttpURLConnection) myURL.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder results = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            results.append(line);
        }

        connection.disconnect();
        System.out.println(results.toString());
    }
}
秋风の叶未落 2025-01-14 00:51:43

对 URL 中的参数进行编码,如下所示:

String messageText = URLEncoder.encode(messageText, "UTF-8");

encode the parameters in the URL as follows:

String messageText = URLEncoder.encode(messageText, "UTF-8");
野却迷人 2025-01-14 00:51:43

我遇到了同样的错误。就我而言,这是因为标头中的 sizjwt 令牌大于 mule 软代理可接受的大小。一种选择是增加 mule soft 中可接受的标头大小,或者通过删除分配给用户 id 的一些权限来减少令牌的大小

I a encountered same error. In my case, it was because the sizjwt token in the header was larger than acceptable size by mule soft proxy. One option is to increase the size of acceptable header size in mule soft, or reduce the size of token by removing some of the permissions assigned to the user id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文