一直试图调试此问题无济于事。
我尝试将自定义标头传递给我的POST请求,但我收到错误请求 - 标题名称无效
这是我的代码:
@Override
protected Void doInBackground(Void... params) {
// Create a new HttpClient and Post Header
httpclient = new DefaultHttpClient();
httppost = new HttpPost(URL_LOGIN);
try {
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair(KEY_EMAIL, etEmail.getText().toString()));
params1.add(new BasicNameValuePair(KEY_PASSWORD, etPassword.getText().toString()));
StringBuilder sbb = new StringBuilder();
sbb.append("{\"s\":");
sbb.append("\"" + session_id + "\",");
sbb.append("\"n\":");
sbb.append("\"" + getHashKey(ts, salt) + "\",");
sbb.append("\"t\":");
sbb.append("\"" + ts + "\"}");
// Set HTTPPOST with your parameters
httppost.setEntity(new UrlEncodedFormEntity(params1));
httppost.setHeader(Constants.DEVICE_HEADER, sbb.toString());
// Execute HTTP Post Request
response = httpclient.execute(httppost);
// Setup an InputStream to handle the response.
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"iso-8859-1"), 8);
// Use a reader to read the data and store it in a String builder
StringBuilder sb = new StringBuilder();
String s;
while(true) {
s = buf.readLine();
if(s==null || s.length()==0)
break;
sb.append(s);
}
buf.close();
ips.close();
Log.d("User Info", sb.toString());
JSONObject jsonObj = new JSONObject(sb.toString());
Log.e("Is successful?", "" + jsonObj.getString(KEY_SUCCESS));
if (jsonObj.getString(KEY_SUCCESS).equals("true")) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(LoginActivity.this, "Login successful!", Toast.LENGTH_SHORT).show();
i = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(i);
}
});
} else {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(LoginActivity.this, "Unauthorized user", Toast.LENGTH_SHORT).show();
}
});
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我的标题名称是设备,但我很难得到这个错误好几天。有任何想法吗?我很乐意感谢你的帮助。谢谢!
这是log cat错误消息:
10-27 11:41:11.383: D/User Info(18162): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><HTML><HEAD><TITLE>Bad Request</TITLE><META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD><BODY><h2>Bad Request - Invalid Header</h2><hr><p>HTTP Error 400. The request has an invalid header name.</p> </BODY></HTML>
答案 0 :(得分:0)
我发现了这个问题。我的一个变量中有一个尾随空格。
所以要修复,
nonce.trim();