/**

* 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. 安装lamp服务器

    1.安装http: $ yum install httpd 2.启动http: $ systemctl start httpd 3.访问:http://192.168.1.100 4.Installi ...

  2. 矩阵指数 Matrix Exponentials

    转自:https://zh.wikipedia.org/wiki/%E7%9F%A9%E9%98%B5%E6%8C%87%E6%95%B0 其中,X. X2.X3…….Xk 都是n阶矩阵,显然 exp ...

  3. HDU4548美素数——筛选法与空间换时间

    对于数论的学习比较的碎片化,所以开了一篇随笔来记录一下学习中遇到的一些坑,主要通过题目来讲解 本题围绕:素数筛选法与空间换时间 HDU4548美素数 题目描述 小明对数的研究比较热爱,一谈到数,脑子里 ...

  4. 使用laravel jwt-auth post提交数据一直出现 'error' => 'invalid_credentials'

    注意,laravel 对密码使用Hash加密,检查一下数据库user表中的password有没有Hash加密过 没仔细看文档坑死我了

  5. Codeforces I. Inna and Nine(组合)

    题目描述: Inna and Nine time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. HBase学习笔记之HBase原理和Shell使用

    HBase学习指南之HBase原理和Shell使用 参考资料: 1.https://www.cnblogs.com/nexiyi/p/hbase_shell.html,hbase shell

  7. 学习Spring-Data-Jpa(十七)---对Web模块的支持

    Spring-Data还提供了Web模块的支持,这要求Web组件Spring-MVC的jar包位于classpath下.通常通过使用@EnableSpringDataWebSupport注解来启用继承 ...

  8. Bagging and Random Forest

    Bagging和随机森林RF. 随机森林是最受欢迎和最强大的机器学习算法之一.它是一种称为Bootstrap Aggregation或bagging的集成机器学习算法. bootstrap是一种强大的 ...

  9. 解决<c:if>无else的问题

    之前发了一个jstl的if标签博客,说是jsp没有提供<c:else>标签.于是有大佬评论,说<c:choose></c:choose>可以解决,通过查资料和敲代码 ...

  10. 2019.12.06 java基础

    JRE:运行环境(包含JVM(Java Virtual Machine)- Java虚拟机和核心类库) JDK: JAVA语言的软件开发工具包(Java Development Kit) Dos命令行 ...