Android HTTP Post Request to IIS 7 Returns Bad Request(Invalid Header Name)

我的梦境 提交于 2019-12-12 05:48:10

问题


I have a MVC 3 Web Sevice being hosted on an Amazon EC2 Instance. I have an android app that is making a post request to the service. However a 400 bad request is returned saying that the header name is invalid. I checked the logs on the server and the request does not make it into IIS. The HTTP Err Log just has these entries:

2011-10-07 02:01:05 xxx.xxx.xx.xx xxxxx xx.xxx.xx.xx 80 HTTP/1.1 POST /API/UserAccount/Login 400 - Header -

Not really sure what is going on. I tested this web service on the developement server that comes with visual studio and there was no problems. Here is the code that creates the post request on Android:

HttpPost post = new HttpPost(LOGIN_URL);
StringEntity se = new StringEntity(json);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
    "application/json"));
post.setEntity(se);
response = client.execute(post);

Any insight is appreciated.

Thanks.


回答1:


Change the way you're setting the Http headers. Here's an updated version of your code:

HttpPost post = new HttpPost(LOGIN_URL);
StringEntity se = new StringEntity(json);
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
post.setEntity(se);
response = client.execute(post);


来源:https://stackoverflow.com/questions/7682514/android-http-post-request-to-iis-7-returns-bad-requestinvalid-header-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!