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:/ ...
随机推荐
- shell部分面试题
1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...
- vue饿了么UI库-笔记
1. :rules="{required: true, message: '有效期不能为空'}" :rules="{type:'date',required: true, ...
- 域权限维持:Ptk(Pass The Ticket)
Kerberos协议传送门:https://www.cnblogs.com/zpchcbd/p/11707302.html 1.黄金票据 2.白银票据
- Task 使用方法
Task的使用方法 1. 调用无参数.无返回值方法 private void button1_Click(object sender, EventArgs e) { Task task = new T ...
- spring boot+Quartz+数据库存储
SpingBoot+Quartz+数据库存储 1.Spring整合Quartz 2.读取数据库中表达式启动定时任务1(每5s执行) 3.更改定时任务状态(启用/禁用),定时任务1停止 4.读取数据库中 ...
- 树组件——jstree使用
本文记录的只是我自己当时的代码,每行的注释很清楚了,你自己可以做相应变通 一.使用前提: 1.下载jstree依赖包 2.相关页面引入样式["jstree/themes/default/st ...
- [RN] React Native 使用 react-native-vector-icons 图标显示问号
我在第一次使用 react-native-vector-icons 时图标显示问号 后来在网上查了很多文章,发现原因有两个 1)安装完 react-native-vector-icons 后,没有li ...
- async、await总结
一.async用法 async作为一个关键字放到函数前面,用于表示函数是一个异步函数.异步函数也就意味着该函数的执行不会阻塞后面代码的执行. 异步函数语法很简单,就是在函数前面加上async 关键字, ...
- 洛谷P1854 花店橱窗布置
题目 DP,直接递推比记忆化搜索简单. 定义状态\(dp[i][j]\)为前i行最后一个选择第i行第j个数所得到最大值. 易得状态转移方程 \(dp[i][j]=max(dp[i-1][k]+a[i] ...
- Python题库系列分享一(17道)
1.1 到Python官方网站下载并安装Python解释器环境.1.2 到Anaconda官方网站下载并安装最新的Anaconda3开发环境.1.3 Python程序的__name__的作用是什 ...