2011-08-18 296 views
1

我想在我的應用程序中使用RESTFul Web服務。ClientProtocolException

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); 

回答

3

嘗試刪除主機和內容長度頭,它爲我工作。

相關問題