gpt4 book ai didi

android - HTTPURLConnection - 400 错误请求

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:13 33 4
gpt4 key购买 nike

我检查这个Post .但我仍然不明白逻辑错误可能是什么。我仍然收到此错误。我试图转储模拟器流量。但我仍然不明白问题可能出在哪里。

从流量转储中,这是 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

我不知道那些额外的字符是什么意思。但我试图从中找出问题所在。

这是基本代码:

String credentials = username + ":" + password;
byte[] toencode = null;
try {
toencode = credentials.getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
HttpURLConnection conn = null;
try {
//Utilities.isNetworkAvailable(context);
URL url = new URL(params[0]);
Log.d(params[0],"UR");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + Base64.encodeToString(toencode, Base64.DEFAULT));
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("Host", "www.domain.com");
conn.setRequestMethod("GET");
conn.setDoOutput(true);

String data = conn.getInputStream().toString();
return data;
}

有什么想法吗?

更新

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

 [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 属性。

有什么想法吗?

最佳答案

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

编辑:我使用的顺序。

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

关于android - HTTPURLConnection - 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12399673/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com