HTTP 接口
HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程。客户端连上 web服务器后,若想获得web服务器中的某个web资源,需遵守一定的通讯格式,HTTP协议用于定义客户端与web服务器通迅的格式;http接口最主要的作用是能够较好地解决不同系统(可能是功能不同、开发语言不同、服务商不同,等等,但都是基于http服务的)之间的交互的需求;
请求方式有:POST、GET、HEAD、OPTIONS、DELETE、TRACE、PUT,常用的有: GET、 POST
返回的内容为json串形式
1:使用场景:
当对方提供接口给我们,有接口地址以及需要传入的参数,而我们需要获得返回的数据时,就可以用这种方法
2:使用种类:
分为GET请求和POST请求,具体使用哪种需要视不同的接口来定
3:实例代码;
1:POST请求:
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 com.weaver.general.BaseBean;
/**
* 发送post的类
* 1:url:接受java的统一资源定位符url 如:http://api.es.xiaojukeji.com/river/Order/get
* 2:param:接受传过来的参数 :如token认证,时间戳,具体参数按照接口定义的来
* 3:利用链接的conn得出输出流,将参数写入到url中
* 4:建立远程资源之间的链接
* 5:利用URLConnection的输入流来读取URl的响应
*/
public class Post {
public static String sendPost(String url, String param) {
//自定义的类,用来写日志
BaseBean bs = new BaseBean();
// bs.writeLog("Post开始:" + dt.getCurrentDate("T"));
//定义输出流
OutputStreamWriter out = null;
//定义输入流
BufferedReader in = null;
//定义放回的字符串
String result = "";
try {
//创建url链接,类 URL
代表一个统一资源定位符,它是指向互联网“资源”的指针
URL realUrl = new URL(url);
//每个HttpURLConnrction实例用于单个请求
HttpURLConnection conn = null;
// 打开和URL之间的连接,返回一个 URLConnection
对象,它表示到 URL
所引用的远程对象的连接。
conn = (HttpURLConnection) realUrl.openConnection();
// 发送POST请求必须设置如下两行
conn.setRequestMethod("POST"); // POST方法
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置通用的请求属性
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/json");
//使用connect()方法来建立和远程资源之间的链接
conn.connect();
//------如果是post请求的话需要加上这个来进行一个post方式的参数的传递-----
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(param);// 发送请求参数
out.flush();// flush输出流的缓冲
//-------post的请求结束-------------------------
// 定义BufferedReader输入流来读取URL的响应。conn.getInputStream来获取URLConnection的输入流
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
bs.writeLog("Post结束:" + dt.getCurrentDate("T"));
}
} catch (Exception e) {
bs.writeLog("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
2:GET请求:(和POST类似,唯一的区别就是get请求不用输出流写参数,但是需要将参数加入到url后)
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.Iterator;
import java.util.Map;
import com.weaver.general.BaseBean;
import net.sf.json.JSONObject;
public class Get {
public static String sendGet(String url, JSONObject jsonObject) {
BaseBean bs = new BaseBean();
bs.writeLog("Get开始:" + dt.getCurrentDate("T"));
//定义输入流,用来写入url返回的参数
BufferedReader in = null;
String result = "";
try {
//遍历参数,将参数加在url后边进行get请求
将得到的jsonObject字符串进行迭代;
Iterator jsonKeys = jsonObject.keys();
url+="?";
while(jsonKeys.hasNext()){
String key = (String) jsonKeys.next();
String Parameter = jsonObject.getString(key);
url = url+key+"="+Parameter+"&";
}
url=url.substring(0, url.length()-1);
bs.writeLog("get请求的url;"+url);
//创建URL链接
URL realUrl = new URL(url);
//每个HttpURLConnrction实例用于单个请求
HttpURLConnection conn = null;
// 打开和URL之间的连接
conn = (HttpURLConnection) realUrl.openConnection();
// 发送GET请求只设置请求方法即可
conn.setRequestMethod("GET"); // GET方法
// 设置通用的请求属性
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/json");
//使用connect()方法来建立和远程资源之间的链接
conn.connect();
// 定义BufferedReader输入流来读取URL的响应。conn.getInputStream来获取URLConnection的输入流
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
// bs.writeLog("Post结束:" + dt.getCurrentDate("T"));
}
} catch (Exception e) {
bs.writeLog("发送 GET 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}