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:/ ...
随机推荐
- C语言基础知识-程序流程结构
C语言基础知识-程序流程结构 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.概述 C语言支持最基本的三种程序运行结构:顺序结构,选择结构,循环结构. 顺序结构:程序按顺序执行, ...
- 关闭firefox火狐浏览器下载完成时自动扫描(49.0.2以后版本)
本人自己找到的方法,亲测有效,如下:1.在火狐浏览器地址里输入about:config回车,可能会提示“这可能使质量保证失效”,点击[我了解此风险!]2.在搜索框里输入browser.safebrow ...
- centos6.5安装crmsh
CentOS默认没有crmsh的yum源,因此可以借用OpenSUSE的源(OpenSUSE的包也是rpm). 操作步骤很简单首先先进入yum源的安装目录,下载repo配置文件,(返回原工作目录,)执 ...
- SpringBoot启动流程及其原理
Spring Boot.Spring MVC 和 Spring 有什么区别? 分别描述各自的特征: Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等:但他们的 ...
- Echo团队Alpha冲刺 - 测试随笔
目录 测试工作的安排 测试工具选择和运用 测试用例文档 测试体会 项目测试评述 测试工作的安排 模块 测试人 测试内容 单元测试 李东权,黄少勇 测试类或者函数是否能正确处理用户请求 接口测试 林弘杰 ...
- Mybatis框架-Delete节点元素的使用
这个就也比较简单,需求:将我们最新插入的那条数据删除掉,从用户表中. UserMapper.xml UserMapper.java 编写测试方法: @Test public void testDele ...
- ReactiveX 学习笔记(30)操作符辨析
RxJava: merge/concat/switch RxJS: merge/concat/switch/exhaust RxSwift: merge/concat/switchLatest mer ...
- LeetCode 1008. Construct Binary Search Tree from Preorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...
- SpringBoot第三节(thymeleaf的配置与SpringBoot注解大全)
Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.所以这里介绍一下Springboot使用Thymeleaf的实例以及遇到的问题. 1.配置与使用 1.1:在applica ...
- vue中代理实现方法
vue中代理实现方法如下: const path = require('path'); function resolve(dir) { return path.join(__dirname, dir) ...