问题
HttpGet request = new HttpGet("https://192.168.1.140:8732/...);
I wonder why I can only send successfully for custom headers : UserName and AuthToken if I do the following:
request.setHeader("User-Agent", "android_client");
request.setHeader("Host", "192.168.1.140:8732");
request.addHeader("UserName", mUserName);
request.addHeader("AuthToken", mAuthorizationToken);
Why is that this code NOT sending the UserName but AuthToken only? When the two bottom lines are reversed.
request.setHeader("User-Agent", "android_client");
request.setHeader("Host", "192.168.1.140:8732");
request.addHeader("AuthToken", mAuthorizationToken);
request.addHeader("UserName", mUserName);
Why is this code failing with 400 error code, invalid hostname when I don't specific the host
// request.setHeader("User-Agent", "android_client");
// request.setHeader("Host", "192.168.1.140:8732");
request.addHeader("UserName", mUserName);
request.addHeader("AuthToken", mAuthorizationToken);
If I don't need to send the UserName and AuthToken, I don't really need to set the Host and it works just fine with the code commented out like following
// request.setHeader("User-Agent", "android_client");
// request.setHeader("Host", "192.168.1.140:8732");
Though I don't think that it is related, I want to disclose that I am using self-signed certificate for these http call from android following this blog. Looking forward to the divine revelation for my poor http soul ...
回答1:
It is a fluke. I couldn't reproduce it anymore. I have been working with the working solution and left it for a while working on different project. I come back and take a look with wire /context logging at oleg suggestion with the help of How to enable logging for apache commons HttpClient on Android I couldn't reproduce the problem anymore. The power of the logging has scared the problem away. Will update if the problem occurs again and if I find out the cause.
来源:https://stackoverflow.com/questions/9712035/adding-multiple-custom-http-request-headers-mystery