post 发送方式
|
/** * 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 发送方式的更多相关文章
- spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式
spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式 背景: 原来我准备是setDefaultDestinationName 设置队列的名称 ...
- HTML中发送方式(method)中get和post的比较
get的方式是将表单控件的控件名name和取值value信息经过编码后,通过URL发送(可以在地址栏里看到).而post则将表单的内容通过http发送.一个 get通过URL传送变量,能传送的数据总量 ...
- JmsTemplate 发送方式
---恢复内容开始--- 背景: 原来我准备是setDefaultDestinationName 设置队列的名称 发现 系统运行后 创建 的并不是队列 ,而是Topic , 自己看下源码,发现在创 ...
- android handler ,message消息发送方式
1.Message msg = Message.obtain(mainHandler) msg.obj=obj;//添加你需要附加上去的内容 msg.what = what;//what消息处理的类 ...
- python实现邮件发送完整代码(带附件发送方式)
实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...
- Android为TV端助力 handler ,message消息发送方式
1.Message msg = Message.obtain(mainHandler) msg.obj=obj;//添加你需要附加上去的内容 msg.what = what;//what消息处理的类 ...
- PHP通过XML报文格式的POST请求方式,与第三方接口交互(发送xml,获取XML,并解析xml步骤)
开发者端:发送请求,并接收结果 <?php // 下面的demo,实现的功能如下: // 1-开发者需要判断一个用户是否存在,去请求第三方接口. // 2-与第三方接口的通信,是以xml格式传送 ...
- (转)获取 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 ...
- RocketMQ(6)---发送普通消息(三种方式)
发送普通消息(三种方式) RocketMQ 发送普通消息有三种实现方式:可靠同步发送.可靠异步发送.单向(Oneway)发送. 注意 :顺序消息只支持可靠同步发送. GitHub地址: https:/ ...
随机推荐
- DB开发规范---初稿
1 公共约定 1.1 存储引擎 默认统一使用InnoDB引擎 1.2 字符集设定 后续新建DB默认使用utf8mb4字符集,校对规则使用utf8mb4_general_bin. 历史DB多使用utf8 ...
- Python基础->for循环、字符串以及元组
python流程控制>for循环.字符串以及元组 学习有关序列的思想.序列:一组有顺序的东西.所有的序列都是由元素组成的,序列中的元素位置是从0开始编号的,最后一个元素的位置是它长度减一. fo ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- python代码规范 自动优化工具Black
自动优化工具Black 在众多代码格式化工具中,Black算是比较新的一个,它***的特点是可配置项比较少,个人认为这对于新手来说是件好事,因为我们不必过多考虑如何设置Black,让 Black 自己 ...
- P2602 [ZJOI2010]数字计数(递推)
P2602 [ZJOI2010]数字计数 思路: 首先考虑含有前导0的情况,可以发现在相同的\(i\)位数中,每个数的出现次数都是相等的.所以我们可以设\(f(i)\)为\(i\)位数每个数的出现次数 ...
- bat echo 每行不同的颜色
bat echo 每行不同的颜色 先看代码: @echo off SETLOCAL EnableDelayedExpansion for /F "tokens=1,2 delims=#&qu ...
- windows 10 下使用Navicat for oracle 数据库还原
一.前期准备 1.安装windows 10系统 2.安装oracle 11g 数据库 3.安装PLsql(也不需要) 4.安装sqlplus(这个必须有) 5.使用下面这个东西新建数据库(不懂创建的话 ...
- 讨论SQL语句中主副表之间的关系
在公司这么多些时间,自己在写SQL语句这方面的功夫实在是太差劲了,有时候自己写出来的SQL语句自己都不知道能不能使用,只是自己写出来的SQL语句是不报错的,但是,这对于真正意义上的SQL语句还差的真的 ...
- MATLAB中运算符优先级
下述运算符的优先级从低到高: 1.先决或(||): 2.先决与(&&): 3.逻辑或(|): 4.逻辑与(&): 5.等于类(<,<=,>,>=,==, ...
- 思科ASA基本配置
------------恢复内容开始------------ ASA基本配置 ciscoasa#show running-config //讲解已作的默认配置 ciscoasa#conf ...