httpclient post get

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpClientTest {

	public static void main(String[] args) throws Exception{
		HttpClient httpClient = new HttpClient();
		HttpMethod method = postMethod("http://localhost:8080/test_server_1/callback.action");
		httpClient.executeMethod(method);
		String response = method.getResponseBodyAsString();
		System.out.println(response);
	}
	
	public static HttpMethod getMethod(String url,String param) throws IOException{
		GetMethod get = new GetMethod(url+"?"+param);
		get.releaseConnection();
		return get;
	}
		
	public static HttpMethod postMethod(String url) throws IOException{ 
		PostMethod post = new PostMethod(url);
		post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");  
		NameValuePair[] param = {
				new NameValuePair("fileInfo","文件名"),
				new NameValuePair("param","参数")	} ;
    	post.setRequestBody(param);
    	post.releaseConnection();
		return post;
	}
}

 

commons-httpclient.jar

你可能感兴趣的:(httpclient,post,get)