httpclient 简单例子

以下代码基于httpclient3.1

public class HttpClientTest {

    public static void main(String args[]) throws Exception {
        HttpClient httpClient = new HttpClient();
        String url = "http://localhost:8080/wm/index.jsp";
        PostMethod postMethod = new PostMethod(url);
        // 填入各个表单域的值
        NameValuePair[] data =new NameValuePair[]{new NameValuePair("uname","zhang san")};
        // 将表单的值放入postMethod中
        postMethod.setRequestBody(data);
       // postMethod.addParameter("uname", "zhang san");一样的效果
         postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0");
        // 执行postMethod
        httpClient.executeMethod(postMethod);
        System.out.println(postMethod.getResponseBodyAsString());
        postMethod.releaseConnection();
    }
}
 

你可能感兴趣的:(httpclient)