HTTP POST GET
(1) 将数据封装为json格式,然后再发送
/**
*
*/
package com.inspur;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import net.sf.json.JSONObject;
/**
ClassName: contect_flask
Function: TODO ADD FUNCTION
@author: xuzheng
date: 2019-7-1 下午2:03:00
*/
public class contect_flask {/**
@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put(“deviceID”, “110”);
jsonObject2.put(“state”, “0”);
jsonObject2.put(“channel”, “channel”);
jsonObject1.put(“item”, jsonObject2);
jsonObject1.put(“requestCommand”, “control”);
String result = contect_flask.post(jsonObject1, “http://172.30.12.188:5003/test/hello”);System.out.println(result);
}
public static String post(JSONObject json, String url){
String result = “”;
HttpPost post = new HttpPost(url);
try{
CloseableHttpClient httpClient = HttpClients.createDefault();post.setHeader("Content-Type","application/json;charset=utf-8");post.addHeader("Authorization", "Basic YWRtaW46");StringEntity postingString = new StringEntity(json.toString(),"utf-8");post.setEntity(postingString);HttpResponse response = httpClient.execute(post);InputStream in = response.getEntity().getContent();BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));StringBuilder strber= new StringBuilder();String line = null;while((line = br.readLine())!=null){strber.append(line+'\n');}br.close();in.close();result = strber.toString();if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){result = "服务器异常";}} catch (Exception e){System.out.println("请求异常");throw new RuntimeException(e);} finally{post.abort();}return result;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
三、Get请求方式
(1) 无参数的Get请求方式
/**
*/
package com.inspur;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import net.sf.json.JSONObject;
/**
ClassName: contect_flask
Function: TODO ADD FUNCTION
@author: xuzheng
date: 2019-7-1 下午2:03:00
*/
public class contect_flask {/**
@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String result = contect_flask.get(“http://172.30.12.188:5003/test/hello”);System.out.println(result);
}
public static String get(String url){
String result = “”;
HttpGet get = new HttpGet(url);
try{
CloseableHttpClient httpClient = HttpClients.createDefault();HttpResponse response = httpClient.execute(get);result = getHttpEntityContent(response);if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){result = "服务器异常";}} catch (Exception e){System.out.println("请求异常");throw new RuntimeException(e);} finally{get.abort();}return result;
}
public static String getHttpEntityContent(HttpResponse response) throws UnsupportedOperationException, IOException{
String result = “”;
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream in = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in, “utf-8”));
StringBuilder strber= new StringBuilder();
String line = null;
while((line = br.readLine())!=null){
strber.append(line+’\n’);
}
br.close();
in.close();
result = strber.toString();
}return result;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(2) 有参数的Get请求方式
/**
*
*/
package com.inspur;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import net.sf.json.JSONObject;
/**
ClassName: contect_flask
Function: TODO ADD FUNCTION
@author: xuzheng
date: 2019-7-1 下午2:03:00
*/
public class contect_flask {/**
@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> map = new HashMap<String, String>();
map.put(“name”, “root”);
map.put(“password”, “123456”);
String result = contect_flask.get(map, “http://172.30.12.188:5003/test/hello”);System.out.println(result);
}
public static String get(Map<String, String> paramMap, String url){
String result = “”;
HttpGet get = new HttpGet(url);
try{
CloseableHttpClient httpClient = HttpClients.createDefault();
List params = setHttpParams(paramMap);
String param = URLEncodedUtils.format(params, “UTF-8”);
get.setURI(URI.create(url + “?” + param));
HttpResponse response = httpClient.execute(get);
result = getHttpEntityContent(response);if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){result = "服务器异常";}} catch (Exception e){System.out.println("请求异常");throw new RuntimeException(e);} finally{get.abort();}return result;
}
public static List setHttpParams(Map<String, String> paramMap){
List params = new ArrayList();
Set<Map.Entry<String, String>> set = paramMap.entrySet();
for(Map.Entry<String, String> entry : set){
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
return params;
}public static String getHttpEntityContent(HttpResponse response) throws UnsupportedOperationException, IOException{
String result = “”;
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream in = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in, “utf-8”));
StringBuilder strber= new StringBuilder();
String line = null;
while((line = br.readLine())!=null){
strber.append(line+’\n’);
}
br.close();
in.close();
result = strber.toString();
}return result;
}
}
最新文章
- 0x000000ed怎么修复_win10蓝屏代码0x000000ed的修复方法
- c#将字符串转换为数组_pandas入门: 时间字符串转换为年月日
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- [云炬创业学笔记]第二章决定成为创业者测试7
- sessionFactory.getCurrent()和sessionFactory.openSession()的区别
- 集合(collection)
- P3-weixin-2.0.1 版本发布,JAVA微信插件框架
- EMLO模板GeMedia媒体范儿[小梦修改尊享版]
- Spring Bean的生命周期例子
- 【报告分享】疫情期间抖音、快手带货趋势分析报告.pdf(附下载链接)
- 【代码笔记】iOS-获得现在的时间
- [ios] - TommyBros(山寨马里奥) – 开源游戏
- 【金三银四】Java基础知识面试题(2021最新版)
- 常规诊疗条件下比较依那西普生物类似药(益赛普)与阿达木、英夫利西对RA的疗效[EULAR2015_SAT0360]...
- PHP pdo查询sqlite,PHP PDO 操作 sqlite数据库 案例
- GUI线程安全详解(三)
- IT程序员的常见病:颈椎病、肩周炎 .
- Java list删除多个指定索引的元素
- R语言 - 安装R及RStudio(Linux、Windows双重记录)
- Genesis公链:夯实Web 3.0发展底座
热门文章
- 数据库服务器如何备份详细教程!
- 什么是研究报告,研究报告分为那些部分
- linux下查看巨杉数据库,SequoiaDB巨杉数据库入门:快速搭建流媒体服务器
- 概率论中常见分布总结以及python的scipy库使用:两点分布、二项分布、几何分布、泊松分布、均匀分布、指数分布、正态分布
- 查询给定区域内曲面平均高程
- 重磅!!微软终于对 JDK 下手了…
- WARNING: disk usage in log directory [/home/.../.ros/log] is over 1GB. 问题解决办法
- 项目一 认识Linux操作系统
- VC6 VS2010 环境变量
- 计算机实践教程作业桌面管理截图,如何将电脑现在的桌面截图,并保存在考试题目里面...