文章目录
http协议一、发送GET请求二、发送POST请求总结http协议
HTTP是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型。HTTP是一个无状态的协议。
提示:以下是本篇文章正文内容,下面案例可供参考
一、发送GET请求
代码如下(示例):
import mons.lang3.StringUtils;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.*;import .HttpURLConnection;import .URL;import .URLConnection;import .URLEncoder;import java.nio.charset.StandardCharsets;import java.util.Map;/*** @author chenwx*/public class HttpUtils {/*** 使用Get方式获取数据, ** @param url URL包括参数,http://HOST/XX?XX=XX&XXX=XXX** @return*/public static String sendGet(String url) {String result = "";BufferedReader in = null;try {URL realUrl = new URL(url);// 打开和URL之间的连接URLConnection connection = realUrl.openConnection();// 设置通用的请求属性connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");connection.setRequestProperty("version", "ems_track_cn_1.0");connection.setRequestProperty("authenticate", "35B05C8E7ED189B4E050030A240B17D1");// 建立实际的连接connection.connect();// 定义 BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("发送GET请求出现异常!" + e);e.printStackTrace();}// 使用finally块来关闭输入流finally {try {if (in != null) {in.close();}} catch (Exception e2) {e2.printStackTrace();}}return result;}}
二、发送POST请求
代码如下(示例):
import mons.lang3.StringUtils;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.*;import .HttpURLConnection;import .URL;import .URLConnection;import .URLEncoder;import java.nio.charset.StandardCharsets;import java.util.Map;/*** @author chenwx*/public class HttpUtils {/*** 发送post请求的raw形式** @param url 路径* @param value 参数值 json串*/public static String sendPostRaw(String url, String value) {try {HttpClient httpClient = new DefaultHttpClient();HttpPost post = new HttpPost(url);post.setHeader("Content-Type", "application/json;charset=utf-8");if(StringUtils.isNotEmpty(value)) {StringEntity postingString = new StringEntity(value, "utf-8");post.setEntity(postingString);}// post.setHeader("authorization", token);HttpResponse response = httpClient.execute(post);String content = EntityUtils.toString(response.getEntity());return content;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "";}}
总结
httpUtil工具类 发送 get post 请求