1、发送doPost请求,在web那边使用request.setCharacterEncoding("UTF-8");保证中文不乱码,不需要第三方jar包

 public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("charset", "utf-8");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}

2、拼装URL参数信息

 public static String buildQueryStr(Map<String, Object> params) {
if (CheckNull.isNull(params)) {
return "";
}
StringBuffer sb = new StringBuffer();
for (String k : params.keySet()) {
sb.append(k);
sb.append("=");
sb.append(params.get(k).toString());
sb.append("&");
}
return sb.toString().substring(0, sb.length() - 1);
}

HttpUtil的更多相关文章

  1. HttpUtil工具类

    HttpUtil工具类 /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=val ...

  2. HttpUtil 【判断网络连接的封装类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 该封装类主要包括以下功能: 判断是否有网络连接.判断是否有可用的网络连接: 判断是否是3G网络.判断mobile网络是否可用: 判断 ...

  3. 利用URLConnection http协议实现webservice接口功能(附HttpUtil.java)

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

  4. 【Hutool】Hutool工具类之Http工具——HttpUtil

    最简单最直接的上手可以参见参考文档:http://hutool.mydoc.io/?t=216015   Http协议的介绍,请参考web随笔:http://www.cnblogs.com/jiang ...

  5. JavaUtil_07_HttpUtil_使用Hutool 封装的 HttpUtil

    二.参考资料 1.[Hutool]Hutool工具类之Http工具——HttpUtil

  6. 自己写的Android端HttpUtil工具类

    package com.sxt.jcjd.util; import java.io.IOException; import java.io.UnsupportedEncodingException; ...

  7. httputil用http获取请求的工具类

    package com.xiaocan.demo.util; import java.io.IOException; import java.io.InputStream; import java.u ...

  8. Hutool工具类之HttpUtil使用Https

    关于Hutool工具类之HttpUtil如何使用可以参考官方文档Hutool之HttpUtil 其实使用Http和Https使用的方式是一样的. 建议大家可以看看HttpUtil的源码,感觉设计的挺不 ...

  9. Apache HttpComponents 工具类 [ HttpUtil ]

    pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...

  10. Android Xutils框架HttpUtil Get请求缓存问题

    话说,今天和服务器开发人员小小的逗逼了一下,为啥呢? 话说今天有个"收藏产品"的请求接口,是get request的哦,我客户端写好接口后,点击"收藏按钮",返 ...

随机推荐

  1. MSSQL的表锁

    DECLARE @PlanId INT; BEGIN TRAN; INSERT INTO TbName(col,col2) VALUES ('sss','2016/11/8 18:25:12'); S ...

  2. SQL语句-创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100 GO USE 库名GO IF EXISTS (SELECT * FRO ...

  3. struts-validate.xml配置详解demo

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC &quo ...

  4. APEX初步

    APEX是SFDC中用于开发的语言.语法上类似JAVA等面向对象的语言,运行起来类似数据库中的存储过程.可以在SFDC事件中添加业务逻辑,操作相关数据和用在Visual Force页面中. 保存,编译 ...

  5. adobe premiere pro cc2015.0已停止工作 解决办法

    adobe premiere pro cc2015.0已停止工作 一直报错 解决办法就是: 删除我的电脑  我的饿文档下的 Adobe下的Premiere Pro文件夹 现象就是怎么重新安装都不管用P ...

  6. [译]Object.getPrototypeOf

    原文 概要 返回指定object的prototype. 语法 Object.getPrototypeOf(object) 参数 object 要返回原型的对象. 描述 当object参数不是一个对象的 ...

  7. [Windows] win7 配置Java开发环境

    Installed Softwares Git for windows 64 bit Java 7 & 8 64 bit apache maven 3.3.3 Intellij Idea ID ...

  8. Velocity 局部定制模板

    Velocity介绍 Velocity是一个基于java的template engine.它允许Web designer引用Java Code中定义的方法.Web designer可以和Java工程师 ...

  9. 计算sql语句的查询时间

    set statistics profile on set statistics io on set statistics time on go <这里写上你的语句...> go set ...

  10. Selector

    原文: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Sele ...