ClientProtocolException

时间:2011-08-18 10:02:35

标签: android web-services exception rest

我想在我的应用程序中使用RESTFul Web服务。

PUT /API/Authentication HTTP/1.1 
HOST: $baseuri:port 
Accept: text/xml 
Content-Type: text/xml; 
charset=utf-8 
Content-Length: nnnn 

<?xml version="1.0" encoding="utf-8"?>
<Myapp xmlns="http://schemas.myapp.com/REST/Requests/05172011/SignIn"> 
<SignInRequest>
 <UID>ME</UID> 
 <PWD>BlackPearl</PWD> 
</SignInRequest> 
</Myapp>

我尝试了一些代码。但它显示ClientProtocolException。 我的代码中有什么错误?请帮我。 谢谢......

  String auth="myname:mypassword";
  String encoded_auth=Base64.encodeToString(auth.getBytes(),Base64.DEFAULT);
  String auth_header= "Authorization: BASIC "+encoded_auth;

  String xml="<?xml version='1.0' encoding='utf-8'?><Myapp xmlns='http://schemas.myapp.com/REST/Requests/05202011/SignIn'><SignInRequest><UID>ME</UID><PWD>BlackPearl</PWD></SignInRequest></Myapp>";
  int l=xml.length();

  StringEntity e = new StringEntity(xml);
  e.setContentType("text/xml");
  e.setContentEncoding("UTF-8");

  HttpPut objPut = new HttpPut(basruri+"/myApp/API/Authentication");
objPut.setHeader("Content-Type","text/xml;charset=utf-8");
objPut.setHeader("Accept","text/xml");
objPut.setHeader("Host",basruri);
objPut.setHeader("Content-Length",Integer.toString(l));
objPut.setHeader("Authorization",auth_header);
objPut.setEntity(e);    

DefaultHttpClient objClient = new DefaultHttpClient();
ResponseHandler objResponse = new BasicResponseHandler();

HttpResponse s=objClient.execute(objPut);

1 个答案:

答案 0 :(得分:3)

尝试删除Host和Content-Length标头,它对我有用。