HttpClient通过HttpPost传递参数

    1. package http;  
        
      import java.util.ArrayList;  
      import java.util.List;  
        
      import org.apache.http.NameValuePair;  
      import org.apache.http.client.HttpClient;  
      import org.apache.http.client.entity.UrlEncodedFormEntity;  
      import org.apache.http.client.methods.HttpPost;  
      import org.apache.http.impl.client.DefaultHttpClient;  
      import org.apache.http.message.BasicNameValuePair;  
        
      public class HttpTests {  
        
          /** 
           * @param args 
           * @throws Exception 
           */  
          public static void main(String[] args) throws Exception {  
              HttpClient httpclient = new DefaultHttpClient();  
              HttpPost httpPost = new HttpPost("******/abc");  
              List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
              nvps.add(new BasicNameValuePair("username", "vip"));  
              nvps.add(new BasicNameValuePair("password", "secret"));  
              httpPost.setEntity(new UrlEncodedFormEntity(nvps));  
              HttpResponse response = httpclient.execute(httpPost);  
              httpclient.getConnectionManager().shutdown();   
              if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
                 String strResult = EntityUtils.toString(response.getEntity()); 
                 System.out.println(strResult); 
              }
          }  
        
      }


你可能感兴趣的:(HttpClient通过HttpPost传递参数)