服务器返回HTTP响应代码:400 [英] Server returned HTTP response code: 400

查看:3531
本文介绍了服务器返回HTTP响应代码:400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

所以我试图从Java中获取它:

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

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

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

我也尝试过:

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

但没有运气。

任何信息很可观。提前致谢。

Any information is appreciable. Thanks in advance.

推荐答案

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

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

我要求的网址是

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

现在浏览器在内部用%20替换榆树街上的噩梦之间的空格并进行解析。这就是请求的服务器可以响应该请求的原因。但是从Java开始,我没有用%20替换那个空格,所以它变成了Bad Request,来源

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.

现在它正在运作。

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

这篇关于服务器返回HTTP响应代码:400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆