2

我必须以 xml 格式向服务器发送请求。我使用 DefaultHttpClient 和 HttpPost(我必须做发布请求)使用 StringEntity 发送 xml 请求,但我得到“需要 401 授权”错误。我搜索并发现需要身份验证(我有用户名和密码),但如何做对我来说是个问题。目前,我正在使用“UsernamePasswordCredentials”和“BasicCredentialsProvider”,但它会抛出“ClientProtocolException”。我无法弄清楚是什么错误的?另外,我读过身份验证有不同的类型-基本和摘要。我怎么知道我的服务器支持什么?以及如何在Android中实现它们。我对这些东西很陌生,请帮忙!

谢谢

4

1 回答 1

0

这就是我发布的方式...不需要身份验证

        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);
于 2010-02-13T13:26:49.460 回答