基于http的restFul请求
1.Java 用HTTP的方式发送JSON报文请求
public String sendPOSTJsonRequest(String path,String query){
HttpURLConnection connection =null;
try {
//定义请求地址
URL url = new URL(path); //url地址
connection = (HttpURLConnection) url.openConnection();
//doInput默认为true,说明可以从服务器获取数据
//设置是否从httpUrlConnection读入,默认情况下是true;
connection.setDoInput(true);
//doOutput默认为false,含义程序不可以从URL拿数据,所有post请求要设置为true
//设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
//http正文内,因此需要设为true, 默认情况下是false;
connection.setDoOutput(true);
//请求方式
connection.setRequestMethod("POST");
//Post 请求不能使用缓存
connection.setUseCaches(false);
//要在HttpURLConnection实例化之前设置HttpURLConnection.setFollowRedirects(false)。
//setFollowRedirects()是静态方法,设置为false后后面实例化的HttpURLConnection对象
//才不会发生重定向,如果在setFollowRedirects()设置为false之前就得到HttpURLConnection对象,
//则该对象的setFollowRedirects()为默认的true。看来setFollowRedircts()的顺序很重要。
connection.setInstanceFollowRedirects(true);
//获取 request 中用POST方式"Content-type"是"application/json"发送的 json 数据
connection.setRequestProperty("Content-Type","application/json");
//链接
connection.connect();
//post参数的方法
//此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,
try (OutputStream os = connection.getOutputStream()) {
//向对象输出流写出数据,这些数据将存到内存缓冲区中
os.write(query.getBytes("UTF-8"));
}
new InputStreamReader(connection.getInputStream()))) {
String lines;
StringBuffer sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
}
resp = sbf.toString();
}
return resp;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection != null){
connection.disconnect();
}
}
return resp;
}
2.Java 用HTTP的方式发送get报文请求
public boolean sendGETRequest(String path,
Map<String, String> params, String encoding) throws Exception {
StringBuilder url = new StringBuilder(path);
url.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(), encoding));// 编码
url.append('&');
}
url.deleteCharAt(url.length() - 1);
HttpURLConnection connection = (HttpURLConnection) new URL(url.toString()).openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == 200) {
return true;
}
return false;
}
3.通过通过HttpClient框架发送POST请求
/**
* HttpClient该框架已经集成在android开发包中
* 此框架封装了很多的工具类,性能比不上自己手写的下面两个方法
* 但是该方法可以提高程序员的开发速度,降低开发难度
*/
private static boolean sendHttpClientPOSTRequest(String path,
Map<String, String> params, String encoding) throws Exception {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();// 存放请求参数
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
//BasicNameValuePair实现了NameValuePair接口
pairs.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding); //pairs:请求参数 encoding:编码方式
HttpPost httpPost = new HttpPost(path); //path:请求路径
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost); //相当于执行POST请求
//取得状态行中的状态码
if (response.getStatusLine().getStatusCode() == 200) {
return true;
}
return false;
}
jar:
学习来源:https://blog.csdn.net/minzhimo4854/article/details/88925461
https://www.cnblogs.com/zjshd/p/9149643.html
https://www.cnblogs.com/wang-yaz/p/9237981.html
https://www.cnblogs.com/longshiyVip/p/5066285.html
https://www.cnblogs.com/lukelook/p/11169219.html
http://www.voidcn.com/article/p-xifhsqww-ys.html
https://blog.csdn.net/qq_26819733/article/details/52752170
https://vimsky.com/examples/detail/java-method-java.net.HttpURLConnection.setDefaultUseCaches.html
https://wenku.baidu.com/view/48f3c5d226fff705cc170ad1.html
https://ask.csdn.net/questions/243757
//获取 request 中用POST方式"Content-type"是"application/json"发送的 json 数据
https://blog.csdn.net/seapeak007/article/details/70320199
//post 的json请求
https://www.cnblogs.com/wqsbk/p/6771045.html
//jar
https://blog.csdn.net/mhqyr422/article/details/79787518
//前段构建请求
https://my.oschina.net/u/3970421/blog/3055110
//理论
https://www.jianshu.com/p/21622d81ab26
//JSON数据交互和RESTful支持
https://blog.csdn.net/qq_37820491/article/details/89682923
//基于不同框架的实现
https://zhuanlan.zhihu.com/p/69285935
https://cloud.tencent.com/developer/article/1552692
https://blog.csdn.net/beijing20110905/article/details/17508177
//发送xml
https://blog.csdn.net/Roben518/article/details/52247970