1. 程式人生 > >commons-httpclient-3.1.jar的一些用處

commons-httpclient-3.1.jar的一些用處

在這個jar包中一有一些重要的方法,其中包括封裝http的請求的功能,本文可以做一個參考;

import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

public class TestLogin {

	public static void main(String[] args) throws Exception, IOException {
		TestLogin.testLogin();
	}
	
	public static void testLogin() throws HttpException, IOException{
		HttpClient client =  new HttpClient();
		String url = "https://reg.163.com/logins.jsp";
		PostMethod method = new PostMethod(url);
		//method.addParameter("type", "1");
		//method.addParameter("url", "");
		//method.addParameter("product", "urs");
		method.addParameter("username","
[email protected]
"); method.addParameter("password", "xxxxxx"); client.executeMethod(method); String response = method.getResponseBodyAsString(); System.out.println(response); } }

前提是要加入一些jar包才能正常執行的,jar包如下