Apache HttpComponents POST提交带参数提交

public class Test {

    public static void main(String[] args) throws IOException {
        
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {

            HttpPost httpost = new HttpPost("http://localhost:8080/task/index.jsp");

            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("IDToken1", "username"));
            nvps.add(new BasicNameValuePair("IDToken2", "password"));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
            
            HttpResponse response = httpclient.execute(httpost);
            HttpEntity entity = response.getEntity();

            System.out.println("Login form get: " + response.getStatusLine());
            EntityUtils.consume(entity);

            System.out.println("Post logon cookies:");
            List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
                }
            }
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }

}

 

posted @ 2013-07-01 16:51  大新博客  阅读(1191)  评论(0编辑  收藏  举报