/**

* 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. Linux操作系统故障排除之Grub信息被删除

    Linux操作系统故障排除之Grub信息被删除 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. [root@yinzhengjie ~]# hexdump -C /dev/sda - ...

  2. Linux-负载均衡HAproxy

    负载均衡之HAProxy 现在常用的三大开源软件负载均衡器分别是Nginx.LVS.HAProxy.三大软件特点如下: LVS负载均衡的特点: ()抗负载能力强,抗负载能力强.性能高.能达到F5硬件的 ...

  3. Java输入流

    import java.util.*;  //java为小写public class TEST{ public static void main(String args[]){ Scanner inp ...

  4. ACAG 0x02-4 费解的开关

    ACAG 0x02-4 费解的开关 对于这道题,我们不难发现如下性质: 每个位置之多被点击一次: 点击的先后顺序不影响结果: 若确定了第$1$行,则接下来可能的点击方案就只有$1$种.具体原因是:当第 ...

  5. 小白式Git使用教程,从0到1

    Git是什么? Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remo ...

  6. AspxGridView行为:分页、分组、排序、过滤、锁定列

    HTML页面 <dx:ASPxGridView ID="grvList" Width="100%" runat="server" Au ...

  7. [Luogu 3794]签到题IV

    Description 题库链接 给定长度为 \(n\) 的序列 \(A\).求有多少子段 \([l,r]\) 满足 \[ \left(\gcd_{l\leq i\leq r}A_i\right) \ ...

  8. Tensorflow细节-P309-高维向量可视化

    import matplotlib.pyplot as plt import tensorflow as tf import numpy as np import os from tensorflow ...

  9. PostgreSQL JSON 处理

    1.JSON类型    PostgreSQL支持JSON和JSONB.这两种类型在使用上几乎完全一致,主要区别是: (1)JSON类型把输入的数据原封不动的存放到数据库中.JSONB类型在存放时把JS ...

  10. tbls ci 友好的数据库文档化工具

    tbls 是用golang 编写的数据库文档化工具,当前支持的数据库有pg.mysql.bigquery 此工具同时提供了变更对比.lint 校验,生成是markdown格式的 简单使用 安装 mac ...