Android HTTPURLConnection-400错误请求

Android HTTPURLConnection-400错误请求,android,http,bad-request,Android,Http,Bad Request,我检查一下。但我仍然不明白逻辑错误可能是什么。我仍然得到这个错误。我试图转储模拟器流量。但我还是不知道问题出在哪里 从流量转储中,这是Android作为请求发送到服务器的内容。您也可以看到响应: GET /Authenticate/ HTTP/1.1 Authorization: Basic asdfasdfasdfas Accept-Charset: UTF-8 Host: www.domain.com User-Agent: Dalvik/1.4.0 (Linux; U; And

我检查一下。但我仍然不明白逻辑错误可能是什么。我仍然得到这个错误。我试图转储模拟器流量。但我还是不知道问题出在哪里

从流量转储中,这是Android作为请求发送到服务器的内容。您也可以看到响应:

 GET /Authenticate/ HTTP/1.1
 Authorization: Basic asdfasdfasdfas

 Accept-Charset: UTF-8
 Host: www.domain.com
 User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.3; sdk Build/GRI34)
 Connection: Keep-Alive
 Accept-Encoding: gzip

 neQPˆ? 6   6   RT 4VRT 5 E (
 »  @ÍCl¦'
 PÙ[    ˜ároP"8"  neQPI "  "  RT 4VRT 5 E
 ¼  @ËVl¦'
 PÙ[    ˜ároP"8«‹  HTTP/1.1 400 Bad Request
 Date: Thu, 13 Sep 2012 04:47:42 GMT
 Server: Apache/2.2.15 (CentOS)
 Content-Length: 310
 Connection: close
 Content-Type: text/html; charset=iso-8859-1

 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 <html><head>
 <title>400 Bad Request</title>
 </head><body>
 <h1>Bad Request</h1>
 <p>Your browser sent a request that this server could not understand.<br />
 </p>
 <hr>
 <address>Apache/2.2.15 (CentOS) Server at www.domain.com Port 80</address>
 </body></html>
 neQPé¬ 6   6   RT 4VRT 5 E (
 ½  @ÍAl¦'
 PÙ[    ™îároP"8 ,  neQPéË @   @   RT 5RT 4V E  (çØ@ @³-
 l¦'Ù[ Páro ™îP )E            neQPéË @   @   RT 5RT 4V E  (çÙ@ @³,
 l¦'Ù[ Páro ™ïP )D            neQPö“
  ©   ©   RT 5RT 4V E  ›k‹@ @¶Á

  ³ër,9Jr ‘Pÿÿ6B  WRTE   w  [   *  ¨­«º[ 09-13 04:47:41.822   446:0x1c7 D/400      ]
  text/html; charset=iso-8859-1Bad Request
有什么想法吗

更新

我检查了Web服务器日志,查看请求是否命中服务器,以及请求是否存在任何问题。这是我从错误日志中看到的:

 [Thu Sep 13 10:05:24 2012] [error] [client 91.222.195.132] client sent HTTP/1.1 request without  hostname (see RFC2616 section 14.23): /Authenticate/
 [Thu Sep 13 23:11:57 2012] [error] [client 91.222.195.132] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /Authenticate/
 [Thu Sep 13 23:12:03 2012] [error] [client 91.222.195.132] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /Authenticate/
但是,我正在为请求设置header属性

有什么想法吗?

试试这个

static final String _url = "http://www.google.com";
static final String charset = "UTF-8";

// to build the query string that will send the message
private static String buildRequestString(String param1,
        String param2, String param3, String param4, String param5)
        throws UnsupportedEncodingException {
    String[] params = new String[5]; //customize this as per your need 
    params[0] = param1;
    params[1] = param2;
    params[2] = param3;
    params[3] = param4;
    params[4] = param5;

    String query = String.format(
            "uid=%s&pwd=%s&msg=%s&phone=%s&provider=%s",
            URLEncoder.encode(params[0], charset),
            URLEncoder.encode(params[1], charset),
            URLEncoder.encode(params[2], charset),
            URLEncoder.encode(params[3], charset),
            URLEncoder.encode(params[4], charset));
    return query;

}

public static void doSomething(String param1, String param2,
        String param3, String param4, String param5) throws Exception {
    // To establish the connection and perform the post request
    URLConnection connection = new URL(_url
            + "?"
            + buildRequestString(param1, param2, param3, param4,
                    param5)).openConnection();
    connection.setRequestProperty("Accept-Charset", charset);
    // This automatically fires the request and we can use it to determine
    // the response status
    InputStream response = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(response));
    // This stores the response code.
    // Any call to br.readLine() after this is null.
    responsecode = br.readLine();
    // And this logs the already stored response code
    Log.d("ServerResponse", responsecode);
    responseInt = Integer.valueOf(responsecode).intValue();
}

这是我自己想出来的。这是设置标题顺序的问题

编辑:我使用的顺序

URL url = new URL(strUrl);  
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", "myhost.com");
conn.setRequestProperty("Authorization", "Basic " + Base64.encodeToString(toencode, Base64.DEFAULT));
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:1.3.1)");
conn.setRequestProperty("Accept-Charset", "UTF-8");

conn.setConnectTimeout (5000) ; 
conn.setDoOutput(true); 
conn.setDoInput(true); 

我也面临这个问题,但我通过更改代码来修复它,现在我使用以下代码行

 BufferedReader reader;
            StringBuffer buffer;
            String res = null;

            try {
                URL url = new URL(request_url);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setReadTimeout(40000);
                con.setConnectTimeout(40000);
                con.setRequestMethod("GET");
                con.setRequestProperty("Accept","application/json");
                con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                con.setDoInput(true);
                con.setDoOutput(true);


                int status = con.getResponseCode();
                InputStream inputStream;
                if (status == HttpURLConnection.HTTP_OK) {
                    inputStream = con.getInputStream();
                } else {
                    inputStream = con.getErrorStream();
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));
                buffer = new StringBuffer();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                res = buffer.toString();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return res;

你的URL的价值是什么?我明白你想说什么。但是我的URL没有任何查询字符串。这只是简单的基本身份验证标头,格式正确。然后您可以使用此查询而无需任何参数。只需提及您的URL
静态最终字符串_URL=“”据我所知,setDoOutput覆盖了他试图用“POST”生成的“GET”。。。尽管如此,我不知道它是否真的有效。。。
 BufferedReader reader;
            StringBuffer buffer;
            String res = null;

            try {
                URL url = new URL(request_url);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setReadTimeout(40000);
                con.setConnectTimeout(40000);
                con.setRequestMethod("GET");
                con.setRequestProperty("Accept","application/json");
                con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                con.setDoInput(true);
                con.setDoOutput(true);


                int status = con.getResponseCode();
                InputStream inputStream;
                if (status == HttpURLConnection.HTTP_OK) {
                    inputStream = con.getInputStream();
                } else {
                    inputStream = con.getErrorStream();
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));
                buffer = new StringBuffer();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                res = buffer.toString();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return res;