我需要使用用户名和密码验证我的服务我已经尝试this但是没有成功可以帮助我
当我打印inlog cat时,我得到了::
更新3 ::
11-04 16:07:58.767: INFO/System.out(1531): Returned ======>>> <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
代码::
DefaultHttpClient UserIDclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("username","password"));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = UserIDclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
Returned = EntityUtils.toString(resEntity);
System.out.println("ans is :: "+ Returned);
System.out.println("Returned ======>>> "+Returned);
答案 0 :(得分:1)
第一件事:
检查“返回”字符串是否为空。
第二件事:
有一件事我注意到你已经写了两次以下的行,在传递用户名和密码之前需要调用execute()方法:
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
Returned = EntityUtils.toString(entity);
相反,您的代码应为:
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Your DATA
nameValuePairs.add(new BasicNameValuePair("ID", "username"));
nameValuePairs.add(new BasicNameValuePair("Password", "password"));
response.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
HttpResponse response1 = httpclient.execute(post);
if(response1.getStatusLine().getStatusCode()==200)
{
HttpEntity resEntity = response1.getEntity();
System.out.println("=========> Response => "+iStream_to_String(resEntitiy.getContent()));
}
//你可以从here获得iStream_to_String。