java httprequest get_Java使用HttpClient、HttpURLConnection、Request执行Get和Post请求

1、使用HttpClient执行Get和Post

1)执行GET请求public static Map sendGet(String sendUrl) {

Map mres = new HashMap();

// 默认配置创建一个httpClient实例

CloseableHttpClient httpClient = HttpClients.createDefault();

// 创建httpGet远程连接实例

HttpGet httpGet = new HttpGet(sendUrl);

// 设置配置请求参数

RequestConfig requestConfig = RequestConfig.custom()

.setConnectTimeout(35000)// 连接主机服务超时时间

.setConnectionRequestTimeout(35000)// 请求超时时间

.setSocketTimeout(60000)// 数据读取超时时间

.build();

// 为httpGet实例设置配置

httpGet.setConfig(requestConfig);

try {

// 执行get请求得到返回对象

CloseableHttpResponse response = httpClient.execute(httpGet);

// 通过返回对象获取返回数据

HttpEntity entity = response.getEntity();

// 通过EntityUtils中的toString方法将结果转换为字符串

String result = EntityUtils.toString(entity, "UTF-8");

mres = (Map) JSONObject.toBean(JSONObject.fromObject(result), Map.class);

} catch (Exception e) {

e.printStackTrace();

} finally {

// 关闭资源

if (null != httpClient) {

try {

httpClient.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

return mres;

}

2)执行Post请求/*

* Create the POST request

*/

HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost("http://example.com/");

// Request parameters and other properties.

List params = new ArrayList();

params.add(new BasicNameValuePair("user", "Bob"));

try {

httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

} catch (UnsupportedEncodingException e) {

// writing error to Log

e.printStackTrace();

}

/*

* Execute the HTTP Request

*/

try {

HttpResponse response = httpClient.execute(httpPost);

HttpEntity respEntity = response.getEntity();

if (respEntity != null) {

// EntityUtils to get the response content

String content = EntityUtils.toString(respEntity);

}

} catch (ClientProtocolException e) {

// writing exception to log

e.printStackTrace();

} catch (IOException e) {

// writing exception to log

e.printStackTrace();

}

2、使用HttpURLConnection执行Get和Post

1)执行Get请求URL url = new URL(urlStr);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 返回结果-字节输入流转换成字符输入流,控制台输出字符

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

StringBuilder sb = new StringBuilder();

String line;

while ((line = br.readLine()) != null) {

sb.append(line);

}

System.out.println(sb);

2)执行Post请求String rawData = "id=10";

String type = "application/x-www-form-urlencoded";

String encodedData = URLEncoder.encode( rawData, "UTF-8" );

URL u = new URL("http://www.example.com/page.php");

HttpURLConnection conn = (HttpURLConnection) u.openConnection();

conn.setDoOutput(true);

conn.setRequestMethod("POST");

conn.setRequestProperty( "Content-Type", type );

conn.setRequestProperty( "Content-Length", String.valueOf(encodedData.length()));

OutputStream os = conn.getOutputStream();

os.write(encodedData.getBytes());

3、使用Request执行Get和Post请求

1)执行Get请求Request.Get("http://somehost/")

.connectTimeout(1000)

.socketTimeout(1000)

.execute().returnContent().asString();

2)执行Post请求

Request.Post("http://www.example.com/page.php")

.bodyForm(Form.form().add("id", "10").build())

.execute()

.returnContent();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值