我有以下Android代码,这给出了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错误?
答案 0 :(得分:0)
您需要放置Content-Type标头
conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");