Android:访问 RESTful Web 服务时出现 400 错误代码

发布于 2024-12-09 04:35:20 字数 825 浏览 0 评论 0原文

我想访问 Restful Web 服务。我希望请求应采用以下格式。

GET /API/Contacts/username HTTP/1.1 
HOST: $baseuri:port 
Accept: text/xml 
Authorization: Basic ZmF0aWdhYmxlIGdlbmVyYXR=

我还通过 HTTPS 协议调用 Web 服务。 以下是我正在使用的代码:

HttpGet get = new HttpGet("https://secure.myapp.com/MyApp/API/Contacts/myname");

  get.addHeader("Accept","text/xml");
  get.addHeader("Authorization","Basic ZmF0aWdhYmxlIGdlbmVyYXR=");          
  get.addHeader("Host","https://secure.myapp.com");
  get.addHeader("Connection Use","HTTP 1.1");

  DefaultHttpClient client =  new DefaultHttpClient();
  ResponseHandler objHandler = new BasicResponseHandler();
  String getResponse = client.execute(get,objHandler); 

但我收到错误:400 错误请求。

我不确定我的代码是否正确。是否需要在标头中显式指定方法(GET、POST 或 PUT)?

请帮我... 想你....

I want to access a Restful web service. I want the request should be in the following format.

GET /API/Contacts/username HTTP/1.1 
HOST: $baseuri:port 
Accept: text/xml 
Authorization: Basic ZmF0aWdhYmxlIGdlbmVyYXR=

And also I am calling the web service though HTTPS protocol.
The folowing is the code I am using :

HttpGet get = new HttpGet("https://secure.myapp.com/MyApp/API/Contacts/myname");

  get.addHeader("Accept","text/xml");
  get.addHeader("Authorization","Basic ZmF0aWdhYmxlIGdlbmVyYXR=");          
  get.addHeader("Host","https://secure.myapp.com");
  get.addHeader("Connection Use","HTTP 1.1");

  DefaultHttpClient client =  new DefaultHttpClient();
  ResponseHandler objHandler = new BasicResponseHandler();
  String getResponse = client.execute(get,objHandler); 

But I am getting an Error : 400 Bad request.

I am not sure whether my code is correct. Is it necessary to specify the method (GET, POST or PUT) explicitly in the header?

Please help me...
Thnking You....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2024-12-16 04:35:20

难道你不应该把 'Basic ZmF0aWdhYmxlIGdlbmVyYXR=' 用双引号括起来吗?
另请检查 Web 服务示例请求和响应,并确保指定了所有内容。

Shouldn't you put 'Basic ZmF0aWdhYmxlIGdlbmVyYXR=' in double quotes?
Also check web service example request and response and make sure that everything is specified.

爱的十字路口 2024-12-16 04:35:20

哦...
那是我的错误。
我没有保留添加标题的顺序。
当我根据请求更改其订单时,它工作正常。

在请求中,HOST 应该是第一个参数。
以下是更正后的代码。

get.addHeader("Host","secure.myapp.com");
get.addHeader("Accept","text/xml");   
get.addHeader("Authorization","Basic ZmF0aWdhYmxlIGdlbmVyYXR=");                
get.addHeader("Connection Use","HTTP 1.1"); 

抱歉打扰大家了...

Ohh...
That was my mistake.
I did not keep the order of adding headers.
When I changed its order according to the Request it is working fine.

In the request the HOST should be the first parameter.
The following is the corrected code.

get.addHeader("Host","secure.myapp.com");
get.addHeader("Accept","text/xml");   
get.addHeader("Authorization","Basic ZmF0aWdhYmxlIGdlbmVyYXR=");                
get.addHeader("Connection Use","HTTP 1.1"); 

Sorry to disturb you all...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文