/**

* 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. Pod生命周期和健康检查

    Pod生命周期和健康检查 Pod的生命周期涵盖了前面所说的PostStart 和 PreStop在内 Pod phase Pod的status定义在 PodStatus对象中,其中有一个phase字段 ...

  2. 动手搭建codecombat服务

    # 因为后面需要使用浏览器访问 127.0.0.1:3000去获取管理员控制权,所以需要一个图形化的界面. yum install net-tools -y   yum groupinstall &q ...

  3. sqlserver 智能提示插件

    文章:SqlServer智能提示插件SQLPrompt 地址:https://blog.csdn.net/u013628152/article/details/83274478

  4. P2606 [ZJOI2010]排列计数

    P2606 [ZJOI2010]排列计数 因为每个结点至多有一个前驱,所以我们可以发现这是一个二叉树.现在我们要求的就是以1为根的二叉树中,有多少种情况,满足小根堆的性质. 设\(f(i)\)表示以\ ...

  5. docker学习5-docker安装tomcat环境和部署war包

    前言 tomcat部署web项目非常方便,把war包放到webapps目录就可以了.本篇使用docker快速搭建一个tomcat环境 下载tomcat镜像 拉取官方最新版tomcat镜像 [root@ ...

  6. root用户ssh可以登录,xftp通过sftp不能登录链接CentOS解决办法

    xftp显示无法连接到xx.xx.xx(服务器地址) 解决办法: 把/etc/ssh/sshd_config文件中的Subsystem sftp /usr/libexec/openssh/sftp-s ...

  7. Pycharm 切换Git 远程分支

    1.git 仓库新建远程分支以后,pycharm 本地没有办法查看到对应的分支,需要切换到本地代码的git所在的目录执行下"git remote update origin --prune” ...

  8. 讲解Flume

    Spark Streaming通过push模式和pull模式两种模式来集成Flume push模式:Spark Streaming端会启动一个基于Avro Socket Server的Receiver ...

  9. 多项式的各类计算(多项式的逆/开根/对数/exp/带余除法/多点求值)

    预备知识:FFT/NTT 多项式的逆 给定一个多项式 F(x)F(x)F(x),请求出一个多项式 G(x)G(x)G(x),满足 F(x)∗G(x)≡1(mod xn)F(x)*G(x) \equiv ...

  10. tensorflow2.0 学习(一)

    虽说是按<TensorFlow深度学习>这本书来学习的,但是总会碰到新的问题!记录下这些问题,有利于巩固知新. 之前学过一些tensorflow1.0的知识,到RNN这章节,后面没有再继续 ...