HttpURLConnection GET请求获得400错误请求 [英] HttpURLConnection GET request getting 400 Bad Request

查看:2696
本文介绍了HttpURLConnection GET请求获得400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HttpURLConnection在Java中使用一些参数执行GET请求。但是,每次我这样做,每次都会收到400:错误的请求。
我需要做些什么才能使它工作?

  String url =http://www.awebsite的.com / apath P1 = V1&安培; P2 = v2和安培; P3 = V3\" ; 
HttpURLConnection conn =(HttpURLConnection)新的URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setUseCaches(false);
conn.setRequestMethod(GET);
conn.setRequestProperty(Host,www.awebsite.com);
conn.setRequestProperty(User-Agent,Mozilla / 4.0);
conn.setRequestProperty(Accept,text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8);
conn.setRequestProperty(Accept-Language,en-us,en; q = 0.5);
conn.setRequestProperty(Accept-Charset,ISO-8859-1,utf-8; q = 0.7,*; q = 0.7);
conn.setRequestProperty(Keep-Alive,115);
conn.setRequestProperty(Connection,keep-alive);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder data = new StringBuilder();
String s =; ((s = br.readLine())!= null)
data.append(s);
String pageData = data.toString();

我试过了:


  • 在整个查询中(在?之后)和值上使用URLEncoder。
  • 设置内容长度标题

  • 设置连接使用输出并将查询作为输出。

解决方案

看来,在这种情况下,HTTP 400被用来不表示请求语法中的错误,但是这里描述了逻辑错误: HTTP 400(错误的请求)的逻辑错误,而不是格式错误的请求语法


I am trying to do a GET request with some parameters in Java using HttpURLConnection. Everytime I do this however, I get a 400: Bad Request each time. What do I need to change to make it work?

String url = "http://www.awebsite.com/apath?p1=v1&p2=v2&p3=v3";
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", "www.awebsite.com");
conn.setRequestProperty("User-Agent", "Mozilla/4.0");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Keep-Alive", "115");
conn.setRequestProperty("Connection", "keep-alive");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder data = new StringBuilder();
String s = "";
while((s = br.readLine()) != null)
    data.append(s);
String pageData = data.toString();

I have tried:

  • Using URLEncoder on the whole query (after the ?) and just on the values.
  • Setting the content length header.
  • Setting the connection to use output and putting the query as the output.

解决方案

It seems that in this case the HTTP 400 is being used not to signify an error in the request syntax, but a logical error as described here: HTTP 400 (bad request) for logical error, not malformed request syntax.

这篇关于HttpURLConnection GET请求获得400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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