java post带参数的标签_POST请求||带参数的POST请求

POST请求

HttpPostTest.java

package cn.itcast.crawler.test;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpPostTest {

public static void main(String[] args) {

//创建HttpClient对象

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建HttpPost对象,设置url访问地址

HttpPost httpPost = new HttpPost("http://www.itcast.cn");

CloseableHttpResponse response = null;

try {

//使用HttpClient发起请求,获取response

response = httpClient.execute(httpPost);

//解析响应

if (response.getStatusLine().getStatusCode() == 200) {

String content = EntityUtils.toString(response.getEntity(), "utf8");

System.out.println(content.length());

}

} catch (IOException e) {

e.printStackTrace();

}finally {

//关闭response

try {

response.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

httpClient.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

?i=20200313192321103.png

带参数的POST请求

71ca1cfc702e8d4e055bf0f577d00fb4.png

HttpPostParamTest.java

package cn.itcast.crawler.test;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

public class HttpPostParamTest {

public static void main(String[] args) throws Exception {

//创建HttpClient对象

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建HttpPost对象,设置url访问地址

HttpPost httpPost = new HttpPost("http://yun.itheima.com/search");

//声明List集合,封装表单中的参数

List params = new ArrayList();

//设置请求地址是:http://yun.itheima.com/search?keys=Java

params.add(new BasicNameValuePair("keys","Java"));

//创建表单的Entity对象,第一个参数就是封装好的表单数据,第二个参数就是编码

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params,"utf8");

//设置表单的Entity对象到Post请求中

httpPost.setEntity(formEntity);

CloseableHttpResponse response = null;

try {

//使用HttpClient发起请求,获取response

response = httpClient.execute(httpPost);

//解析响应

if (response.getStatusLine().getStatusCode() == 200) {

String content = EntityUtils.toString(response.getEntity(), "utf8");

System.out.println(content.length());

}

} catch (IOException e) {

e.printStackTrace();

}finally {

//关闭response

try {

response.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

httpClient.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

标签:http,请求,client,参数,org,apache,import,POST,response

来源: https://blog.csdn.net/qq_39368007/article/details/104847687

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值