我有以下的Android代码,这给出了一个400错误:的Android HttpURLConnection的给予400错误
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
MySQLiteHelper db = new MySQLiteHelper(this);
String token = db.getLoginToken();
conn.setRequestProperty("Authorization", "Bearer " + token);
OutputStream os = conn.getOutputStream();
os.close();
int responseCode=conn.getResponseCode();
此代码给了我一个400响应代码。我试图复制我们的iOS应用程序代码,即:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *authorization = [NSString stringWithFormat:@"Bearer %@",token];
[request setValue:authorization forHTTPHeaderField:@"Authorization"];
[request setURL:[NSURL URLWithString:professionalConnections]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
而且没有问题。
为什么Android代码会导致400错误?
的问题是在要发送的数据,在这种情况下,URL。参见:[10状态码定义](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html):10.4.1 400错误请求 服务器由于格式错误而无法理解请求句法。 – zaph
@Lorenzo编辑,为什么android代码有400错误,或者我怎样才能进一步调试 –