zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Apache HttpComponents POST提交带参数提交

Apache 参数 提交 post
2023-09-11 14:16:57 时间
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();
        }
    }

}