java发送post及get请求
来源:互联网 发布:银翼杀手剧情解析 知乎 编辑:程序博客网 时间:2024/06/07 00:21
、写一个HttpRequestUtils工具类,包括post请求和get请求
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
91
92
93
94
95
96
97
98
99
100
101
102
103
package
com.koolear.util.httpclient;
import
net.sf.json.JSONObject;
import
org.apache.commons.httpclient.HttpStatus;
import
org.apache.http.HttpResponse;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.client.methods.HttpPost;
import
org.apache.http.entity.StringEntity;
import
org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.util.EntityUtils;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
java.io.IOException;
import
java.net.URLDecoder;
public
class
HttpRequestUtils {
private
static
Logger logger = LoggerFactory.getLogger(HttpRequestUtils.
class
);
//日志记录
/**
* httpPost
* @param url 路径
* @param jsonParam 参数
* @return
*/
public
static
JSONObject httpPost(String url,JSONObject jsonParam){
return
httpPost(url, jsonParam,
false
);
}
/**
* post请求
* @param url url地址
* @param jsonParam 参数
* @param noNeedResponse 不需要返回结果
* @return
*/
public
static
JSONObject httpPost(String url,JSONObject jsonParam,
boolean
noNeedResponse){
//post请求返回结果
DefaultHttpClient httpClient =
new
DefaultHttpClient();
JSONObject jsonResult =
null
;
HttpPost method =
new
HttpPost(url);
try
{
if
(
null
!= jsonParam) {
//解决中文乱码问题
StringEntity entity =
new
StringEntity(jsonParam.toString(),
"utf-8"
);
entity.setContentEncoding(
"UTF-8"
);
entity.setContentType(
"application/json"
);
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url,
"UTF-8"
);
/**请求发送成功,并得到响应**/
if
(result.getStatusLine().getStatusCode() ==
200
) {
String str =
""
;
try
{
/**读取服务器返回过来的json字符串数据**/
str = EntityUtils.toString(result.getEntity());
if
(noNeedResponse) {
return
null
;
}
/**把json字符串转换成json对象**/
jsonResult = JSONObject.fromObject(str);
}
catch
(Exception e) {
logger.error(
"post请求提交失败:"
+ url, e);
}
}
}
catch
(IOException e) {
logger.error(
"post请求提交失败:"
+ url, e);
}
return
jsonResult;
}
/**
* 发送get请求
* @param url 路径
* @return
*/
public
static
JSONObject httpGet(String url){
//get请求返回结果
JSONObject jsonResult =
null
;
try
{
DefaultHttpClient client =
new
DefaultHttpClient();
//发送get请求
HttpGet request =
new
HttpGet(url);
HttpResponse response = client.execute(request);
/**请求发送成功,并得到响应**/
if
(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
/**读取服务器返回过来的json字符串数据**/
String strResult = EntityUtils.toString(response.getEntity());
/**把json字符串转换成json对象**/
jsonResult = JSONObject.fromObject(strResult);
url = URLDecoder.decode(url,
"UTF-8"
);
}
else
{
logger.error(
"get请求提交失败:"
+ url);
}
}
catch
(IOException e) {
logger.error(
"get请求提交失败:"
+ url, e);
}
return
jsonResult;
}
}
2、接收Http请求
接收post请求
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
@Controller
@RequestMapping
(value =
"/api/platform/exceptioncenter/exceptioninfo"
)
public
class
ExceptionInfoController {
//注入
@Autowired
private
ExceptionInfoBiz exceptionInfoBiz;
/**
* 创建异常信息请求
* @param requestBody 请求消息内容
* @param request 请求消息头
* @return jsonObject
*/
@RequestMapping
(
value=
"/create"
,
method = RequestMethod.POST
)
public
ModelAndView createExceptionInfo(
@RequestBody
String requestBody, HttpServletRequest request) {
JSONObject jsonObject = JSONObject.fromObject(requestBody);
ComExceptionInfo comExceptionInfo =
new
ComExceptionInfo();
comExceptionInfo.setProjectName(jsonObject.getString(
"projectName"
));
comExceptionInfo.setTagName(jsonObject.getString(
"tagName"
));
exceptionInfoBiz.insert(comExceptionInfo);
//返回请求结果
JSONObject result=
new
JSONObject();
result.put(
"success"
,
"true"
);
return
new
ModelAndView(
""
, ResponseUtilsHelper.jsonSuccess(result.toString()));
}
}
接收get请求
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
@Controller
@RequestMapping
(value=
"/api/platform/messagecenter/messages/sms"
)
public
class
SmsController {
@Autowired
SmsSendBiz smsSendBiz;
/**
* 接收手机号码和内容往短信发送表插入一条记录
* @param requestbody 请求消息内容
* @param request 请求消息头
* @return jsonObject
*/
@RequestMapping
(
value=
"/send"
,
method= RequestMethod.GET
)
public
ModelAndView sendSms(
@RequestBody
String requestbody, HttpServletRequest request) {
//获取请求URL及url后面传输的参数
String url = request.getRequestURL() +
"?"
+ request.getQueryString();
url = BuildRequestUrl.decodeUrl(url);
String telePhone = RequestUtils.getStringValue(request,
"telePhone"
);
String content = RequestUtils.getStringValue(request,
"content"
);
smsSendBiz.insertTtMsQuequ(telePhone,content);
return
new
ModelAndView(
""
, ResponseUtilsHelper.jsonResult(
""
,
true
));
}
}
0 0
- java发送post及get请求
- Java发送Get/Post请求
- Java发送GET,POST请求
- JAVA发送POST+GET请求
- java发送get/post请求
- Java 发送 get, post 请求
- JAVA 发送 POST、GET请求
- Java发送get,post请求
- java发送GET、POST请求
- Java 发送get post请求
- JAVA 发送 POST、GET请求
- java 发送post,get请求及空格问题处理
- Java使用HttpURLConnection发送Get及Post请求简单过程
- JAVA 发送Http请求,GET、POST请求
- java发送HTTP GET POST请求
- 使用Java发送GET、POST请求
- 使用Java发送POST、GET请求【转】
- 使用Java发送POST、GET请求
- Content Editor Webpart(二)添加JQuery和html代码
- 求阶乘+估计运行时间
- Juniper路由器&防火墙 -- 基本配置方法
- typedef和#define的用法与区别
- Android Listview设置不同布局
- java发送post及get请求
- 从计算器增加“开方”算法浅析简单工厂and工厂方法模式
- OA的学习--第三天的内容--Struts的整理回顾
- 短信上行和下行区别
- 将 Vi/Vim 中,行尾的 ^M 去掉
- Newton's interpolation Note
- 【学习笔记】前端工程化-大公司里怎样开发和部署前端代码
- Kendo+Bootstrap 开发出不一样的强大用户体验的漂亮的UI
- python学习笔记一 基础