向指定URL 发送POST请求的方法
java发送psot请求:
package com.tea.web.admin; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; public class PsotTest { public static void main(String[] args) {
HashMap<String, Object> params = new HashMap<>();
params.put("customerType", 2);
params.put("token", "3d88f30fb4d129027ca5f9c04ed5b733");
params.put("sign", "94257c28f256fbb66b4efd889d1d09be2");
String result = sendPost("http://api.teayk.com/sys/api/customerGrade/findGradeByType", params);
System.err.println(result);
}
private static String sendPost(String url, Map<String, Object> params) {
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
HttpURLConnection conn =(HttpURLConnection) new URL(url).openConnection();
//发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");//POST方法
//设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.connect();
//获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
//发送请求参数
if (params != null) {
/*
StringBuilder param = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
if(param.length()>0){
param.append("&");
}
param.append(entry.getKey());
param.append("=");
param.append(entry.getValue());
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("param:"+param.toString());
out.write(param.toString());
*/ JSONObject json =(JSONObject) JSON.toJSON(params);
System.out.println(json);
out.write(json.toString());
}
out.flush();// flush输出流的缓冲
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){ out.close();}
if(in!=null){in.close();}
}catch(IOException ex){
ex.printStackTrace();
}
}
return result.toString();
}
}
向指定URL 发送POST请求的方法的更多相关文章
- 向指定URL发送GET、POST方法的请求
/** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name ...
- Ajax在静态页面中向指定url发送json请求获取返回的json数据
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- wemall doraemon中Android app商城系统向指定URL发送GET方法的请求代码
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...
- HttpSenderUtil向指定 URL 发送POST方法的请求
package com.founder.ec.common.utils; import java.io.BufferedReader; import java.io.IOException; impo ...
- 向指定URL发送GET方法获取资源,编码问题。 Rest风格
http编码.今天遇到获取网页上的数据,用HTTP的GET请求访问url获取资源,网上有相应的方法.以前一直不知道什么事rest风格,现在我想就是开一个Controller,然后使人可以调用你的后台代 ...
- 向指定URL发送GET和POST请求
package com.sinosoft.ap.wj.common.util; import java.io.BufferedReader;import java.io.IOException;imp ...
- 向指定url发送Get/Post请求
向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 2.HttpUtil 工具类–向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 impor ...
- 发送http请求的方法
在http/1.1 协议中,定义了8种发送http请求的方法 get post options head put delete trace connect patch. 根据http协议的设计初衷,不 ...
- 对WEB url 发送POST请求
package com.excellence.spark; import java.util.List; import com.excellence.spark.test.test; import c ...
随机推荐
- (转)pycharm autopep8配置
转:https://blog.csdn.net/BobYuan888/article/details/81943808 1.pip下载安装: 在命令行下输入以下命令安装autopep8 pip ins ...
- Solidworks 2019 无法获得下列许可solidworks standard无效的(不一致的)使用许可号码(-8,544,0)
若出现如下述错误,只需将C:\***(C盘中生成注册表的文件夹)\Program Files\SOLIDWORKS Corp\SOLIDWORKS下的netapi32.dll文件复制到所安装路径中SO ...
- java 图片裁剪代码
package com.actionsoft.apps.addons.invoice.pc.test; import java.awt.image.BufferedImage;import java. ...
- ORA-02298问题处理
参考:http://blog.163.com/yvtong@126/blog/static/8753524720132223343722/ ORA-39083: Object type REF_CON ...
- T2691 桶哥的问题——送桶
这个题其实不难,就是按照结束时候的顺序从大到小走一遍,能送的就送,如果区间不重合就更新一下 代码: #include<iostream> #include<cstdio> #i ...
- MySql 5.7.20 绿色版安装
MySql 5.7.20 绿色版安装 一.MySql 安装 1.从官网下载绿色压缩包. 2.解压安装文件到指定目录 3.创建配置文件 my.ini 到解压文件的根目录,my.ini 配置文件如下,需将 ...
- cmath模块——复数域数学函数模块
cmath——复数域数学函数模块 转自:https://blog.csdn.net/zhtysw/article/category/7511293 该模块属于内置模块,随时可以调用.它提供了数学函数在 ...
- Delphi Canvas的FillRect(const Rect: TRect) 函数的作用
http://blog.163.com/zhangzhifeng688@126/blog/static/165262758201131211341460/ Delphi Canvas的FillRect ...
- Vagrant 手册之 box - 版本
原文地址 从 Vagrant 1.5 版本起,box 开始支持版本.这运行创建 box 的人提交更新,使用 box 的人检查更新.更新 box 并查看变更记录. 对于 Vagrant 新手,box 版 ...
- PHP Yii框架中使用smarty模板
第一种方法 按照YII系统的办法生成视图觉得有点麻烦,觉得用smarty更省事.尝试着把smarty模板加进来了. date_default_timezone_set("PRC") ...