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请求的方法的更多相关文章

  1. 向指定URL发送GET、POST方法的请求

    /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name ...

  2. Ajax在静态页面中向指定url发送json请求获取返回的json数据

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  3. wemall doraemon中Android app商城系统向指定URL发送GET方法的请求代码

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

  4. HttpSenderUtil向指定 URL 发送POST方法的请求

    package com.founder.ec.common.utils; import java.io.BufferedReader; import java.io.IOException; impo ...

  5. 向指定URL发送GET方法获取资源,编码问题。 Rest风格

    http编码.今天遇到获取网页上的数据,用HTTP的GET请求访问url获取资源,网上有相应的方法.以前一直不知道什么事rest风格,现在我想就是开一个Controller,然后使人可以调用你的后台代 ...

  6. 向指定URL发送GET和POST请求

    package com.sinosoft.ap.wj.common.util; import java.io.BufferedReader;import java.io.IOException;imp ...

  7. 向指定url发送Get/Post请求

    向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 2.HttpUtil 工具类–向指定url发送Get/Post请求 1.向指定url发送Get/Post请求 impor ...

  8. 发送http请求的方法

    在http/1.1 协议中,定义了8种发送http请求的方法 get post options head put delete trace connect patch. 根据http协议的设计初衷,不 ...

  9. 对WEB url 发送POST请求

    package com.excellence.spark; import java.util.List; import com.excellence.spark.test.test; import c ...

随机推荐

  1. [CSP-S模拟测试97]题解

    A.小盆友的游戏 感觉题解解释的很牵强啊……还是打表找规律比较靠谱 对于每个人,它构造了一个期望函数$f(x)$,设它的跟班个数为$cnt[x]$,那么令$f(x)=2^{cnt[x]}-1$(??鬼 ...

  2. eureka注册中心wro.css wro.js 404

    注册中心和配置中心放在一个module里面,如果不配置配种中心的访问前缀,会被config拦截.所以改动如下: package com.cloud.stagging.lhcloudeureka; im ...

  3. 获取小程序accessToken

    private static String getAccessToken(){ String url = "https://api.weixin.qq.com/cgi-bin/token? ...

  4. python web自动化测试框架搭建(功能&接口)——接口测试模块

    Python接口测试采用python读取excel的方法,通过requests库发送请求和接收响应.模块有: Data:用于存放excel用例的,用例格式: iutil: 接口公共方法,数据引擎.ht ...

  5. MySQL 导入导出 CSV 文件

    导入 导出 清空表中的所有数据 注意事项 常见问题 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-pri ...

  6. 第 13 章 python并发编程之io模型

    一.IO模型介绍 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问 ...

  7. HTML--JS 多列求和

    <html> <head> <title>多列求和</title> <script type="text/javascript" ...

  8. 同余dp

    先验知识: 余数的计算公式:c = a -⌊ a/b⌋ * b 其中,⌊ ⌋为向下取整运算符,向下取整运算称为Floor,用数学符号⌊ ⌋表示 题目: Consider an arbitrary se ...

  9. jQuery基础--创建节点和 添加节点

    创建节点 $(function () { // var box = document.getElementById("box"); // // var a = document.c ...

  10. eclipse以及myeclipse的xml配置文件没有提示的问题解决

    对于在使用hibernate时,需要对配置文件进行配置,我们需要引入dtd约束文件.在有网的情况下,可以直接从网上下载,编写xml配置文件的时候,可以提示:在没网的情况下,那么就提示不出来. 下面的方 ...