模拟表单提交

来源:互联网 发布:silverlight5 for mac 编辑:程序博客网 时间:2024/05/20 21:48
httpclient模拟表单提交
  1. 创建httpclient对象,相当于创建浏览器。
    CloseableHttpClient client = HttpClients.createDefault();

  2. 创建请求方法,这里以post方法为例。
    HttpPost httpPost = new HttpPost("http://localhost:8080/csdn/admin/login")

  3. 设置表单提交的相关参数。
    List<NameValuePair> list = new ArrayList<NameValuePair>();    list.add(new BasicNameValuePair("username", "json"));    list.add(new BasicNameValuePair("password", "123"));

  4. 创建表单实体,设置字符编码等。
    UrlEncodedFormEntity uyrlEntity = new UrlEncodedFormEntity(list, "utf-8");

  5. 设置请求实体(是这样讲吗),执行请求并处理response对象。
    httpPost.setEntity(uyrlEntity);     CloseableHttpResponse response =   client.execute(httpPost);     HttpEntity entity1 = response.getEntity();      InputStream io1 =entity1.getContent();     InputStreamReader isReader1 = new InputStreamReader(io1, "utf-8");     BufferedReader bfReader1 = new BufferedReader(isReader1);     String string1 = "",str1;     while((str1=bfReader1.readLine())!=null){     string1+=str1;     }     System.out.println(string1);

0 0