向服务器发送post请求
2024-09-02 15:55:52
/**
* 通过HttpClient发送Post请求
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
List<NameValuePair> pairs = new ArrayList<NameValuePair>();//存放请求参数
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding);
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(entity);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200){
return true;
}
return false;
}
/**
* 发送Post请求
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
// title=liming&timelength=90
StringBuilder data = new StringBuilder();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
data.append(entry.getKey()).append("=");
data.append(URLEncoder.encode(entry.getValue(), encoding));
data.append("&");
}
data.deleteCharAt(data.length() - 1);
}
byte[] entity = data.toString().getBytes();//生成实体数据
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);//允许对外输出数据
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entity);
if(conn.getResponseCode() == 200){
return true;
}
return false;
}
/**
* 发送GET请求
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
// http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
StringBuilder url = new StringBuilder(path);
url.append("?");
for(Map.Entry<String, String> entry : params.entrySet()){
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(), ecoding));
url.append("&");
}
url.deleteCharAt(url.length() - 1);
HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
return true;
}
return false;
}
最新文章
- 【代码笔记】iOS-柱状图
- SPOJ 375 Query on a tree 树链剖分模板
- 总结30个CSS选择器
- topcoder SRM 618 DIV2 MovingRooksDiv2
- Android学习笔记(八)
- mysql innobackupex xtrabackup 大数据量 备份 还原
- php修改排序,上移下移
- 如何安装mysql-5.5.29-win32.zip
- App Store 加急审核方式
- js渐变显示渐变消失
- 【转】[c/c++ ]字节序与大小端转换--不错
- TC基础使用指南(基于xbeta的TC配置文件)
- AccessibilityService+WindowManager+SurfaceView开系统权限
- 第七章 函数表达式和函数声明,关于this对象 ,私有作用域(function(){})() ,私有变量和特权方法
- emWin监护仪界面设计,含uCOS-III和FreeRTOS两个版本
- dede织梦动态页面通过手机模板实现wap浏览
- Navicat的使用技巧
- 12.DataGrid的columns的特性
- 楼层验证B1~1F等
- Windows安装TensorFlow遇到错误
热门文章
- skiasharp在阿里云Windows server 2016上部署时提示The type initializer for &#39;SkiaSharp.SKAbstractManagedStream&#39; threw an exception. 错误
- 脚本_检测 MySQL 数据库连接数量
- 阿里腾讯校招Java面试题总结及答案
- fastjson 1.1.1填坑
- 数据库与缓存:3.mongodb的基本知识
- java资料搜索网站
- C#-弄懂泛型和协变、逆变
- 获取mysql数据库表字段的备注信息
- Yii2 kineditor
- basic play