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:/ ...
随机推荐
- Java入门(二)——泛型
如果你写过前端,可能会经常写一下关于变量类型的判断,比如:typeof fn === 'function'之类的代码.因为JavaScript作为一门弱类型语言,类型的判断往往需要开发人员自己去检查. ...
- VLAN实验5:利用三层交换机实现VLAN间路由
实验环境: 实验拓扑图: 实验编址: 实验步骤:1.基本配置按照实验编址表在PC上进行基本的IP地址配置,三层交换机上先不做任何配置. 测试PC1与PC2的连通性 正常 测试PC1与PC3的连通性 ...
- Python入门篇-装饰器
Python入门篇-装饰器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.装饰器概述 装饰器(无参) 它是一个函数 函数作为它的形参 返回值也是一个函数 可以使用@functi ...
- Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用
Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...
- uiautomator2+python自动化测试2-查看app页面元素利器weditor
前言 android sdk里面自带的uiautomatorviewer.bat可以查看手机app上的元素,但是不太好用,网上找了个大牛写的weditor,试用了下还是蛮不错的 python环境:3. ...
- luoguU60884 【模板】动态点分治套线段树
题目连接:https://www.luogu.org/problemnew/show/U60884 题意:有N个点,标号为1∼N,用N−1条双向带权通道连接,保证任意两个点能互相到达. Q次询问,问从 ...
- Ubuntu 14.04.2升级openssh7.9
环境:Ubuntu 14.04.2 目的:openssh版本6.6升级为openssh7.9 准备以下3个包 http://www.zlib.net/ https://www.openssl.org/ ...
- WORD添加批注(JAVA)
import com.spire.doc.*;import com.spire.doc.documents.CommentMark;import com.spire.doc.documents.Com ...
- H5中实现加载更多的逻辑及代码执行。
H5中加载更多的逻辑总结: 1.首先,需要三个底部的提示,分别是“加载中”.“--我是有底线的--”.“暂时没有记录”,当然,这三句话根据不同的项目,可以自定义.具体代码例子如下: <div c ...
- repo源
[sdata-base] name=Base baseurl=http://xx.xx.xx.xx:4507/repo/$releasever/$basearch/base enabled=1 gpg ...