我必须以 xml 格式向服务器发送请求。我使用 DefaultHttpClient 和 HttpPost(我必须发送请求)使用 StringEntity 发送 xml 请求,但我收到“需要 401 授权”错误。我搜索并我发现需要身份验证(我有用户名和密码),但如何做对我来说是个问题。目前,我正在使用“UsernamePasswordCredentials”和“BasicCredentialsProvider”,但它抛出“ClientProtocolException”。我无法确定出了什么问题?另外,我已经读到身份验证有不同的类型——基本的和摘要的。我怎么知道我的服务器支持什么?以及如何在 Android 中实现它们。我对这些东西很陌生,请帮忙。!
谢谢
最佳答案
这就是我 POST 的方式...不需要身份验证
DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
HttpPost postMethod=new HttpPost("http://mydomain.com/myfile.php";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//These are the values you are posting
nameValuePairs.add(new BasicNameValuePair("name", username.getText().toString() ));
nameValuePairs.add(new BasicNameValuePair("username", username.getText().toString() ));
nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString() ));
nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString() ));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String response=hc.execute(postMethod,res);
关于android - Android中的身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2257197/