Java http 请求三种形式
2018-03-20 12:01
134 查看
JavaHttp接口交互的两种方式
JavaHttp接口开发针对表单提交跟Json提交的两种方式
表单提交(post、get)
get表单参数提交
/**
* @param type
* @param url
* @param map
* @return
* GY
* 2018年1月2日
* url:请求地址
* map:请求参数
* Get有参请求
* @throws IOException
* @throws ClientProtocolException
*/
public static String
HttpSendGet(String url, Map<String,String> map)
throws ClientProtocolException,
IOException{
String resultStr = null;
if(map==null){
return resultStr;
}
CloseableHttpClient httpclient =
HttpClients.createDefault();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String,
String> entry : map.entrySet())
{
//给参数赋值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String paramStr =
EntityUtils.toString(new UrlEncodedFormEntity(formparams,
Consts.UTF_8));
HttpGet httpget = new HttpGet(url+"?"+paramStr);
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
post表单提交
/**
* @param url
* @param map
* @return
* GY
* 2018年1月2日
* url:请求地址
* map:请求参数
* Post有参请求
* @throws IOException
* @throws ClientProtocolException
*/
public static String
HttpSendPost(String url, Map<String,String> map)
throws ClientProtocolException,
IOException{
if(map==null){
return null;
}
String resultStr = null;
//1.获得一个httpclient对象
CloseableHttpClient httpclient =
HttpClients.createDefault();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String,
String> entry : map.entrySet())
{
//给参数赋值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
Consts.UTF_8);
//System.out.println(EntityUtils.toString(entity));
//2.生成一个post请求
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);
//3.执行post请求并返回结果
CloseableHttpResponse response = httpclient.execute(httppost);
//4.处理结果,这里将结果返回为字符串
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
get无参提交
/**
* 发送HttpGet请求
* @param url
* @return
* @throws IOException
* @throws ClientProtocolException
* GY
* 2018年1月2日
* Get无参请求
*/
public static String
sendGet(String url) throws ClientProtocolException,
IOException {
//1.获得一个httpclient对象
CloseableHttpClient httpclient =
HttpClients.createDefault();
//2.生成一个get请求
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
//3.执行get请求并返回结果
response = httpclient.execute(httpget);
String result = null;
//4.处理结果,这里将结果返回为字符串
HttpEntity entity = response.getEntity();
if (entity != null)
{
result =
EntityUtils.toString(entity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return result;
}
Json数据类型提交
/**
* @param url
* @param json
* @return
* @throws ClientProtocolException
* @throws IOException
* GY
* 2018年1月4日
* 发送json 数据 http client
*/
public static String
HttpSendJsonPost(String url, String json)
throws ClientProtocolException,
IOException{
if(StringUtils.isEmpty(json)){
return null;
}
String resultStr = null;
//1.获得一个httpclient对象
CloseableHttpClient httpclient =
HttpClients.createDefault();
StringEntity postingString = new StringEntity(json,
Consts.UTF_8);//
json传递
postingString.setContentEncoding("UTF-8");
postingString.setContentType("application/json");
System.out.println(EntityUtils.toString(postingString));
//2.生成一个post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(postingString);
//3.执行post请求并返回结果
CloseableHttpResponse response = httpclient.execute(httpPost);
//4.处理结果,这里将结果返回为字符串
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
JavaHttp接口开发针对表单提交跟Json提交的两种方式
表单提交(post、get)
get表单参数提交
/**
* @param type
* @param url
* @param map
* @return
* GY
* 2018年1月2日
* url:请求地址
* map:请求参数
* Get有参请求
* @throws IOException
* @throws ClientProtocolException
*/
public static String
HttpSendGet(String url, Map<String,String> map)
throws ClientProtocolException,
IOException{
String resultStr = null;
if(map==null){
return resultStr;
}
CloseableHttpClient httpclient =
HttpClients.createDefault();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String,
String> entry : map.entrySet())
{
//给参数赋值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String paramStr =
EntityUtils.toString(new UrlEncodedFormEntity(formparams,
Consts.UTF_8));
HttpGet httpget = new HttpGet(url+"?"+paramStr);
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
post表单提交
/**
* @param url
* @param map
* @return
* GY
* 2018年1月2日
* url:请求地址
* map:请求参数
* Post有参请求
* @throws IOException
* @throws ClientProtocolException
*/
public static String
HttpSendPost(String url, Map<String,String> map)
throws ClientProtocolException,
IOException{
if(map==null){
return null;
}
String resultStr = null;
//1.获得一个httpclient对象
CloseableHttpClient httpclient =
HttpClients.createDefault();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String,
String> entry : map.entrySet())
{
//给参数赋值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
Consts.UTF_8);
//System.out.println(EntityUtils.toString(entity));
//2.生成一个post请求
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);
//3.执行post请求并返回结果
CloseableHttpResponse response = httpclient.execute(httppost);
//4.处理结果,这里将结果返回为字符串
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
get无参提交
/**
* 发送HttpGet请求
* @param url
* @return
* @throws IOException
* @throws ClientProtocolException
* GY
* 2018年1月2日
* Get无参请求
*/
public static String
sendGet(String url) throws ClientProtocolException,
IOException {
//1.获得一个httpclient对象
CloseableHttpClient httpclient =
HttpClients.createDefault();
//2.生成一个get请求
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
//3.执行get请求并返回结果
response = httpclient.execute(httpget);
String result = null;
//4.处理结果,这里将结果返回为字符串
HttpEntity entity = response.getEntity();
if (entity != null)
{
result =
EntityUtils.toString(entity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return result;
}
Json数据类型提交
/**
* @param url
* @param json
* @return
* @throws ClientProtocolException
* @throws IOException
* GY
* 2018年1月4日
* 发送json 数据 http client
*/
public static String
HttpSendJsonPost(String url, String json)
throws ClientProtocolException,
IOException{
if(StringUtils.isEmpty(json)){
return null;
}
String resultStr = null;
//1.获得一个httpclient对象
CloseableHttpClient httpclient =
HttpClients.createDefault();
StringEntity postingString = new StringEntity(json,
Consts.UTF_8);//
json传递
postingString.setContentEncoding("UTF-8");
postingString.setContentType("application/json");
System.out.println(EntityUtils.toString(postingString));
//2.生成一个post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(postingString);
//3.执行post请求并返回结果
CloseableHttpResponse response = httpclient.execute(httpPost);
//4.处理结果,这里将结果返回为字符串
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr =
EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
相关文章推荐
- 解决Fiddler无法捕获本地HttpWebRequest(C#.net)请求和HttpURLConnection(Java)请求
- 南哥带你学 Java 之 JavaEE 设置全局配置、请求转发、请求重定向、HttpServletResponse 和 HttpServletRequest
- java实现 HTTP/HTTPS请求绕过证书检测代码实现
- JAVA POST请求远程HTTP接口
- java发送http的get、post请求
- java httpclient发送post get 请求
- spring MVC 管理HttpClient---实现在java中直接向Controller发送请求
- HTTP POST请求报文格式分析与Java实现文件上传
- JAVA发送http get/post请求,调用http接口、方法
- java http 请求方法
- 如何在java中发起http和https请求 配置信任
- Java实现http(post、get)请求
- Java利用HttpURLConnection发送post请求上传文件
- java发送http的get、post请求
- HTTP POST请求报文格式分析与Java实现文件上传
- [Java] 模拟HTTP的Get和Post请求
- JavaWeb开发-用Apache的HttpClient4.5完成HttpGet请求
- Java httpclient请求,解决乱码问题
- java发送http的get、post请求
- java模拟http的Get/Post请求,并设置ip与port代理