0

我需要在我的应用程序中做简单的 http post。

找到示例并创建AsyncTask类。post的主要代码是这样的:

nameValuePairs- 是帖子元素

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL_STRING);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
String data = new BasicResponseHandler().handleResponse(response);   

我是怎么得到这个异常的

org.apache.http.client.HttpResponseException: Forbidden

这是什么意思?如果该服务返回此内容,那么如何查看完整消息?

另外,如果有其他方法可以制作 http 帖子,我可以试试 :)

谢谢你们的帮助。

4

1 回答 1

0

异常org.apache.http.client.HttpResponseException表示非 2xx HTTP 响应,如下所述:http: //hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpResponseException.html

您可以使用简单的 httpPOST 方法,如下所示:

         HttpClient httpclient = new DefaultHttpClient();    
         HttpPost httppost = new HttpPost("http://Your URL/");      
        try {        
         // Add your data        
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);        
         nameValuePairs.add(new BasicNameValuePair("Name1", "Value1"));        
         nameValuePairs.add(new BasicNameValuePair("Name2", "Value2"));     
         nameValuePairs.add(new BasicNameValuePair("Name3", "Value3"));    

          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));          
        // Execute HTTP Post Request        
         HttpResponse response = httpclient.execute(httppost);             
         } 
        catch (ClientProtocolException e) 
        {       
         // TODO Auto-generated catch block    
         } 
        catch (IOException e) 
        {         
        // TODO Auto-generated catch block  
          }
于 2012-05-30T09:48:41.913 回答