HttpClient新版本(新老版本没有做明确界定,Sorry!)
HttpClient 老版本:
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));
httpclient.execute(httpPost);
httpclient.getConnectionManager().shutdown();
}
}
HttpClient 老版本:
package http;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpTests {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
HttpClient httpclient = new HttpClient();
PostMethod httpPost =new PostMethod("******/abc");
NameValuePair[] param = { new NameValuePair("username", "vip")};
httpPost.setRequestBody(param);
httpclient.executeMethod(httpPost);
}
}