【工具】java发送GET、POST请求
前项目使用这种HTTP的方式进行数据交互,目前已更换数据交互方式,但是作为接口提供调用来说还是比较简洁高效的:
总体流程就是:
1、发送HTTP请求
2、获取返回的JSON对象
3、JSON转换
package com.xx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class SendGetPost {
/**
* 发送get请求
* 参数设置:?param=val¶m2=val2
* @author TF
*
*/
public static String sendGet(String url){
String result = "";
BufferedReader in = null;
try {
//创建URL对象、
URL urlGet =new URL(url);
//打开URL连接
URLConnection connection = urlGet.openConnection();
//设置请求头信息
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
connection.setRequestProperty("contentType", "UTF-8");
//建立连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 发送post请求
* 参数设置param1=val1&m2=val2
*
* */
public static String sendPost(String url,String param){
String result = "";
BufferedReader in = null;
OutputStreamWriter out = null;
try {
//创建URL对象、
URL urlPost = new URL(url);
//打开URL连接
URLConnection connection = urlPost.openConnection();
HttpURLConnection conn = (HttpURLConnection) connection;
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (MalformedURLException e) {
System.out.println("error");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
【工具】java发送GET、POST请求的更多相关文章
- Java发送HTTP POST请求示例
概述: http请求在所有的编程语言中几乎都是支持的,我们常用的两种为:GET,POST请求.一般情况下,发送一个GET请求都很简单,因为参数直接放在请求的URL上,所以,对于PHP这种语言,甚至只需 ...
- java发送http get请求的两种方式
长话短说,废话不说 一.第一种方式,通过HttpClient方式,代码如下: public static String httpGet(String url, String charset) thro ...
- Java 发送http post 请求
package com.sm.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.i ...
- java 发送get,post请求
package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...
- Java发送HTTP POST请求(内容为xml格式)
今天在给平台用户提供http简单接口的时候,顺便写了个调用的Java类供他参考. 服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的h ...
- java 发送http json请求
public void getRemoteId(HttpServletRequest request,Model model){ String name = request.getParameter( ...
- JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理
java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...
- 通过java发送http请求
通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...
- JAVA发送http get/post请求,调用http接口、方法
import java.io.BufferedReader; import java.io.IOException;import java.io.InputStream; import java.io ...
- 用JAVA发送一个XML格式的HTTP请求
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
随机推荐
- what is the difference between definition and declaration in c
A declaration introduces an identifier and describes its type, be it a type, object, or function. A ...
- build-qt.sh(Cross compile in Linux for Windows)
#!/bin/bash set -e MINGW=${MINGW:-${ARCH:-x86_64}-w64-mingw32} PREFIX=${PREFIX:-usr} WORKSPACE=${WOR ...
- 构建自己的PHP框架(composer)
完整项目地址:https://github.com/Evai/Aier Composer 利用 PSR-0 和 PSR-4 以及 PHP5.3 的命名空间构造了一个繁荣的 PHP 生态系统.Compo ...
- DataTable转换为Entity(反射&&泛型)
public static IEnumerable<T> Parse<T>(IEnumerable<DataRow> rows) where T : class, ...
- 向WPF的Grid里面增加控件
在c#中,有时需要通过代码创建面板控件,但是对于面板的操作与之前的控件并不是很直观,因此记下方法,以供后来复习使用: 创建两行两列的表格: 第一种方法: Grid grid = new Grid(); ...
- WPF 绑定到集合
<Window x:Class="CollectionBinding.MainWindow" xmlns="http://schemas.micros ...
- SQLite 的版本问题
原文:SQLite 的版本问题 在SQLite官方网站上的下载包真可以看花眼.不同的.net版本,还有不同的平台,开发和发布时需要加以注意. 在网上搜了搜,早有人注意到了. 关于在.Net开发中使用S ...
- 自定义LISTBOX内子项为checkbox或者radio时,关于IsChecked绑定
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}&q ...
- Django 创建超级管理员失败
django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1") 解决方案 在执行mi ...
- cordova-plugin-local-notifications发送Android本地消息
原文:cordova-plugin-local-notifications发送Android本地消息 1.GitHub源代码地址: https://github.com/katzer/cordova- ...