关于HTTP调用接口的四种方式(post,get,delete,put)
来源:互联网 发布:淘宝龙瞎皮肤多少钱 编辑:程序博客网 时间:2024/05/16 12:20
package Utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
public class smsUtils {
//POST请求
public static String httpURLConnectionPOST (String parm,String Url) {
try {
URL url = new URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中
connection.setDoOutput(true); // 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)
connection.setDoInput(true); // 设置连接输入流为true
connection.setRequestMethod(“POST”); // 设置请求方式为post
connection.setUseCaches(false); // post请求缓存设为false
connection.setInstanceFollowRedirects(true); // 设置该HttpURLConnection实例是否自动执行重定向
connection.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded;charset=utf-8”);
connection.connect(); // 建立连接
// 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
dataout.writeBytes(parm);
// 输出完成后刷新并关闭流
dataout.flush();
dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)
// 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader)
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), “utf-8”));
String line;
StringBuilder sb = new StringBuilder(); // 用来存储响应数据
// 循环读取流,若不到结尾处
while ((line = bf.readLine()) != null) {
sb.append(line).append(System.getProperty(“line.separator”));
}
bf.close();
connection.disconnect(); // 销毁连接
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//GET请求 public static String httpURLConectionGET(String URLParam) { try { URL url = new URL(URLParam); // 把字符串转换为URL请求地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接 connection.connect();// 连接会话 // 获取输入流 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) {// 循环读取流 sb.append(line); } br.close();// 关闭流 connection.disconnect();// 断开连接 return sb.toString(); } catch (Exception e) { e.printStackTrace(); System.out.println("失败!"); } return null; } //delete请求 public static String DELETE(String url,Map<String,String> dataForm){ HttpClient httpClient = new HttpClient(); DeleteMethod deleteMethod = new DeleteMethod(url); List<NameValuePair> data = new ArrayList<NameValuePair>(); if(dataForm!=null){ Set<String> keys = dataForm.keySet(); for(String key:keys){ NameValuePair nameValuePair = new NameValuePair(key, (String) dataForm.get(key)); data.add(nameValuePair); } } deleteMethod.setQueryString(data.toArray(new NameValuePair[0])); try { int statusCode = httpClient.executeMethod(deleteMethod); if (statusCode != HttpStatus.SC_OK) { return "Method failed: " + deleteMethod.getStatusLine(); } byte[] responseBody = deleteMethod.getResponseBody(); return new String(responseBody); } catch (IOException e) { e.printStackTrace(); }finally { deleteMethod.releaseConnection(); } return null; } //put 请求 public static String httpURLConnectionPut (String parm,String Url) { try { URL url = new URL(Url); HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中 connection.setDoOutput(true); // 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数) connection.setDoInput(true); // 设置连接输入流为true connection.setRequestMethod("PUT"); // 设置请求方式为post connection.setUseCaches(false); // post请求缓存设为false connection.setInstanceFollowRedirects(true); // 设置该HttpURLConnection实例是否自动执行重定向 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); connection.connect(); // 建立连接 // 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容) DataOutputStream dataout = new DataOutputStream(connection.getOutputStream()); dataout.writeBytes(parm); // 输出完成后刷新并关闭流 dataout.flush(); dataout.close(); // 重要且易忽略步骤 (关闭流,切记!) // 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader) BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); String line; StringBuilder sb = new StringBuilder(); // 用来存储响应数据 // 循环读取流,若不到结尾处 while ((line = bf.readLine()) != null) { sb.append(line).append(System.getProperty("line.separator")); } bf.close(); connection.disconnect(); // 销毁连接 return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
}
- 关于HTTP调用接口的四种方式(post,get,delete,put)
- http的四种请求方法:get, put, post, delete
- http的4种提交方式简要介绍(get,post,put,delete)
- 关于HTTP中GET,POST,PUT,DELETE的区别。
- http 的get,put,post,delete
- HTTP的get、put、post、delete
- get,post,delete,put的http请求。
- Http Get Post put delete
- Http Get Post put delete
- Http Get Post put delete
- HTTP GET POST PUT DELETE
- Http Get Post put delete
- http post get put delete
- Http请求访问方式 GET PUT POST DELETE
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
- WEB API 中HTTP的get、post、put,delete 请求方式
- spring,springmvc,mybatis,AJAX集成+alibaba的连接池druid
- 【教程】PDF控件Spire.PDF 教程:在C#中加密和解密PDF文件
- Java与JSON之间的相互转换-4
- 普通程序员该如何进阶为全栈工程师?
- 跨学科视域下的机器智能与人
- 关于HTTP调用接口的四种方式(post,get,delete,put)
- 【设计模式】单例模式(Singleton Pattern)
- leetcode Letter Combinations of a Phone Number
- ios oc与js的交互总结
- 2017年第18届立嘉国际机械展览会会刊(参展商名录)
- 区间dp||记忆化搜索 Game of Sum UVA
- 扫描分析代码漏洞的强大工具
- vue.js使用axios实现跨域请求
- 复合主键与联合主键