/**

* post 方式 解码

*/

public static String getWebContentByPost(String urlString, String data,

final String charset, int timeout) throws IOException {

if (urlString == null || urlString.length() == 0) {

return null;

}

urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern();

URL url = new URL(urlString);

System.out.println("url=="+url);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 设置是否向connection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

// Post 请求不能使用缓存

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset="+charset+"");

// 增加报头,模拟浏览器,防止屏蔽

connection.setRequestProperty("User-Agent",

"Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");

// 只接受text/html类型,当然也可以接受图片,pdf,*/*任意

connection.setRequestProperty("Accept", "*/*");// text/html

connection.setConnectTimeout(timeout);

connection.connect();

DataOutputStream out = new DataOutputStream(connection

.getOutputStream());

byte[] content = data.getBytes(charset);// +URLEncoder.encode("中文 ",

out.write(content);

out.flush();

out.close();

try {

// 必须写在发送数据的后面

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

return null;

}

} catch (IOException e) {

e.printStackTrace();

return null;

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

connection.getInputStream(), charset));

String line;

StringBuffer sb = new StringBuffer();

while ((line = reader.readLine()) != null) {

sb.append(line).append("\r\n");

}

if (reader != null) {

reader.close();

}

if (connection != null) {

connection.disconnect();

}

System.out.println(sb.toString());

return URLDecoder.decode(sb.toString(), "utf-8");

}

public static String getUndecodeByPost(String urlString, String data,

final String charset, int timeout) throws IOException {

if (urlString == null || urlString.length() == 0) {

return null;

}

urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern();

URL url = new URL(urlString);

System.out.println("url=="+url);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 设置是否向connection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

// Post 请求不能使用缓存

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset="+charset+"");

// 增加报头,模拟浏览器,防止屏蔽

connection.setRequestProperty("User-Agent",

"Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");

// 只接受text/html类型,当然也可以接受图片,pdf,*/*任意

connection.setRequestProperty("Accept", "*/*");// text/html

connection.setRequestProperty("Content-Length", "*/*");// text/html

connection.setConnectTimeout(timeout);

connection.connect();

DataOutputStream out = new DataOutputStream(connection

.getOutputStream());

byte[] content = data.getBytes(charset);// +URLEncoder.encode("中文 ",

out.write(content);

out.flush();

out.close();

try {

// 必须写在发送数据的后面

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

return null;

}

} catch (IOException e) {

e.printStackTrace();

return null;

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

connection.getInputStream(), charset));

String line;

StringBuffer sb = new StringBuffer();

while ((line = reader.readLine()) != null) {

sb.append(line).append("\r\n");

}

if (reader != null) {

reader.close();

}

if (connection != null) {

connection.disconnect();

}

return sb.toString();

}

post 发送方式的更多相关文章

  1. spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式

    spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式 背景: 原来我准备是setDefaultDestinationName 设置队列的名称 ...

  2. HTML中发送方式(method)中get和post的比较

    get的方式是将表单控件的控件名name和取值value信息经过编码后,通过URL发送(可以在地址栏里看到).而post则将表单的内容通过http发送.一个 get通过URL传送变量,能传送的数据总量 ...

  3. JmsTemplate 发送方式

    ---恢复内容开始--- 背景: 原来我准备是setDefaultDestinationName 设置队列的名称 发现 系统运行后  创建 的并不是队列 ,而是Topic  , 自己看下源码,发现在创 ...

  4. android handler ,message消息发送方式

    1.Message msg =  Message.obtain(mainHandler) msg.obj=obj;//添加你需要附加上去的内容 msg.what = what;//what消息处理的类 ...

  5. python实现邮件发送完整代码(带附件发送方式)

    实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...

  6. Android为TV端助力 handler ,message消息发送方式

    1.Message msg =  Message.obtain(mainHandler) msg.obj=obj;//添加你需要附加上去的内容 msg.what = what;//what消息处理的类 ...

  7. PHP通过XML报文格式的POST请求方式,与第三方接口交互(发送xml,获取XML,并解析xml步骤)

    开发者端:发送请求,并接收结果 <?php // 下面的demo,实现的功能如下: // 1-开发者需要判断一个用户是否存在,去请求第三方接口. // 2-与第三方接口的通信,是以xml格式传送 ...

  8. (转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据

    request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody ...

  9. RocketMQ(6)---发送普通消息(三种方式)

    发送普通消息(三种方式) RocketMQ 发送普通消息有三种实现方式:可靠同步发送.可靠异步发送.单向(Oneway)发送. 注意 :顺序消息只支持可靠同步发送. GitHub地址: https:/ ...

随机推荐

  1. Python入门篇-返回值和作用域

    Python入门篇-返回值和作用域 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.返回值 1>.返回值概述 Python函数使用return语句返回“返回值” 所有函数都 ...

  2. 团队最后一次——冲刺+Beta发布

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign/ 这个作业要求在哪里 https:// ...

  3. 【云栖社区002-二分估值法】要求不用数学库,求 sqrt (2)精确到小数点后10位(Java版)

    如题 初步审题的时候,想到的是暴力搜索:初步设置一个合法的种子,依次按照1e-2,1e-3,1e-4,1e-5,1e-6 , 1e-7...暴力搜索,额,就是太麻烦了. 打比赛搜索写多了,一看见题目就 ...

  4. Appache Flume 中文介绍(转)

    Flume 是什么        Apache Flume是一个高可靠.高可用的分布式的海量日志收集.聚合.传输系统.它可以从不同的日志源采集数据并集中存储. Flume也算是Hadoop生态系统的一 ...

  5. 11 open source business models

    https://www.zdnet.com/article/11-open-source-business-models/ Critics are always claiming open sourc ...

  6. 基于Helm和Operator的K8S应用管理

    https://blog.csdn.net/RancherLabs/article/details/79483013 大家好,今天我们分享的内容是基于Helm和Operator的K8S应用管理. 我们 ...

  7. App版本更新接口的设计

    前段时间公司业务调整,新开了新的移动端的项目,所以和朋友聊到了“版本号”和“版本更新所需的数据表设计”. 一般来讲大部分的软件版本号分3段,比如 A.B.C A 表示大版本号,一般当软件整体重写,或出 ...

  8. 学习Spring-Data-Jpa(十一)---抓取策略与实体图

    1.抓取策略 在前面说到的关联关系注解中,都有一个fetch属性,@OneToOne.@ManyToOne中都默认是FetchType.EAGER,立即获取.@OneToMany.@ManyToMan ...

  9. jupyter Notebook 设置密码

    由于服务器关闭了图形界面 所以在服务器上安装Jupyter Notebook 随后本机web访问,利用本机的显卡可以执行plt相关图形命令 本次介绍如何设置Jupyter Notebook的密码设定 ...

  10. packr 方便的潜入静态资源文件到golang 二进制文件中

    类似的工具以前有介绍过statik,今天使用的工具是packr 也是很方便的golang tools 安装 go get -u github.com/gobuffalo/packr/packr 或者我 ...