/**
* 通过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;
}

最新文章

  1. 【代码笔记】iOS-柱状图
  2. SPOJ 375 Query on a tree 树链剖分模板
  3. 总结30个CSS选择器
  4. topcoder SRM 618 DIV2 MovingRooksDiv2
  5. Android学习笔记(八)
  6. mysql innobackupex xtrabackup 大数据量 备份 还原
  7. php修改排序,上移下移
  8. 如何安装mysql-5.5.29-win32.zip
  9. App Store 加急审核方式
  10. js渐变显示渐变消失
  11. 【转】[c/c++ ]字节序与大小端转换--不错
  12. TC基础使用指南(基于xbeta的TC配置文件)
  13. AccessibilityService+WindowManager+SurfaceView开系统权限
  14. 第七章 函数表达式和函数声明,关于this对象 ,私有作用域(function(){})() ,私有变量和特权方法
  15. emWin监护仪界面设计,含uCOS-III和FreeRTOS两个版本
  16. dede织梦动态页面通过手机模板实现wap浏览
  17. Navicat的使用技巧
  18. 12.DataGrid的columns的特性
  19. 楼层验证B1~1F等
  20. Windows安装TensorFlow遇到错误

热门文章

  1. skiasharp在阿里云Windows server 2016上部署时提示The type initializer for &#39;SkiaSharp.SKAbstractManagedStream&#39; threw an exception. 错误
  2. 脚本_检测 MySQL 数据库连接数量
  3. 阿里腾讯校招Java面试题总结及答案
  4. fastjson 1.1.1填坑
  5. 数据库与缓存:3.mongodb的基本知识
  6. java资料搜索网站
  7. C#-弄懂泛型和协变、逆变
  8. 获取mysql数据库表字段的备注信息
  9. Yii2 kineditor
  10. basic play