httpclient 간단 한 예

1011 단어 httpclient
다음 코드 는 httpclient 3.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();
    }
}

좋은 웹페이지 즐겨찾기