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. div 上禁止复制的css实现方法

    div { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-se ...

  2. (最小割)Path

    http://acm.hdu.edu.cn/showproblem.php?pid=6582 思路:找到最短路核心边建图,跑一遍最小割,最短路核心边的定义为设起点到每个点的最短距离为d[i],每个点到 ...

  3. 5 October

    POJ2676 Sudoku 位运算 + 搜索.更好的优化方法:方案数最小的空格先填. 把某一位 置为 0:a &=~ (1<<n) 把某一位 置为 1:a |= (1<&l ...

  4. [CSP-S模拟测试]:reverse(数位DP)

    题目描述 我们定义: $\overline{d_k...d_2d_1}=\sum \limits_{i=1}^kd_i\times {10}^{i-1}=n(d_i\in [0,9]\ and\ d_ ...

  5. 破解 MyEclipse For Spring 的步骤

    破解 MyEclipse For Spring 的步骤: 1.关闭myeclipse: 2.运行破解工具,写上UserCode,最好是 8 位以上, 3.注意选择 myeclipse 的版本,我提供的 ...

  6. (转)Servlet 3.0/3.1 中的异步处理学习

    转:https://www.cnblogs.com/davenkin/p/async-servlet.html 在Servlet 3.0之前,Servlet采用Thread-Per-Request的方 ...

  7. 常用的Android关键词定位方法

    1字符串,特征字 根据程序运行中出现的特征字词进行搜索,从而获取定位到程序的相关位置之中.以前用 得比较多,不过现在一般难以找到想要的关键词.有时候需要对特征字进行拆分来进行搜索.才 能获得一点提示. ...

  8. 题解[SCOI2009]粉刷匠 难度:省选/NOI-

    Description windy有 N 条木板需要被粉刷.每条木板被分为 M 个格子.每个格子要被刷成红色或蓝色.windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色.每个格子最多 ...

  9. C# 中定义斜杠 \

    \ 是转义符 如     \’ 单引号    \” 双引号    \\ 反斜杠    \0 空    \a 警告(产生峰鸣)    \b 退格    \f 换页    \n 换行    \r 回车   ...

  10. 如何创建linux虚拟机

    一.安装配置linux虚拟机 第1步:运行"Vmware WorkStation",看到主页面. 第2步:创建新的虚拟机,新建虚拟机向导——典型(推荐). 第3步:选择稍后安装操作 ...