java如何发送https请求_java如何发起https请求

48304ba5e6f9fe08f3fa1abda7d326ab.png

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

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

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

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

/*

* 利用HttpClient进行post请求的工具类

*/

public class HttpClientUtil {

public String doPost(String url,Map map,String charset){

HttpClient httpClient = null;

HttpPost httpPost = null;

String result = null;

try{

httpClient = new SSLClient();

httpPost = new HttpPost(url);

//设置参数

List list = new ArrayList();

Iterator iterator = map.entrySet().iterator();

while(iterator.hasNext()){

Entry elem = (Entry) iterator.next();

list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));

}

if(list.size() > 0){

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);

httpPost.setEntity(entity);

}

HttpResponse response = httpClient.execute(httpPost);

if(response != null){

HttpEntity resEntity = response.getEntity();

if(resEntity != null){

result = EntityUtils.toString(resEntity,charset);

}

}

}catch(Exception ex){

ex.printStackTrace();

}

return result;

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值