广州网站ui设计,crm登录系统,接app推广的单子在哪接,做app网站需要什么使用HttpClient发送请求的一般步骤 (1) 创建HttpClient对象。 (2)创建请求方法的实例#xff0c;并指定请求URL。如果需要发送GET请求#xff0c;创建HttpGet对象#xff1b;如果需要发送POST请求#xff0c;创建HttpPost对象。 (3) 如果需要发送请求参数#xff0c;可调用…使用HttpClient发送请求的一般步骤 (1) 创建HttpClient对象。 (2)创建请求方法的实例并指定请求URL。如果需要发送GET请求创建HttpGet对象如果需要发送POST请求创建HttpPost对象。 (3) 如果需要发送请求参数可调用HttpGet同的setParams(HetpParams params)方法来添加请求参数对于HttpPost对象而言可调用setEntity(HttpEntity entity)方法来设置请求参数。 (4) 调用HttpClient对象的execute(HttpUriRequest request)发送请求该方法返回一个HttpResponse。 (5) 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头调用HttpResponse的getEntity()方法可获取HttpEntity对象该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。 (6) 释放连接。无论执行方法是否成功都必须释放连接
下面分别介绍使用HTTPClient和CloseableHTTPClient进行Get和Post请求的方式。
HttpClient
使用commons-httpclient.jarmaven依赖如下 dependencygroupIdcommons-httpclient/groupIdartifactIdcommons-httpclient/artifactIdversion3.1/version/dependencyjava示例代码如下
package com.example.study.demo.http;import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;import java.io.IOException;/*** HttpClient使用示例* jar包commons-httpclient.jar*/
public class HttpClientTest {private static void doGet() {HttpClient client new HttpClient();GetMethod getMethod new GetMethod(https://www.thepaper.cn/);int code 0;try {code client.executeMethod(getMethod);if (code 200) {String res getMethod.getResponseBodyAsString();System.out.println(res);}} catch (IOException e) {e.printStackTrace();}}private static void doPost() {String praiseUrl https://www.thepaper.cn/www/commentPraise.msp; // 澎湃新闻评论点赞urlHttpClient client new HttpClient();PostMethod postMethod new PostMethod(praiseUrl);// 必须设置下面这个HeaderpostMethod.addRequestHeader(User-Agent, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36);postMethod.addParameter(commentId, 18718372); // 评论的id抓包获得try {int code client.executeMethod(postMethod);if (code 200) {String res postMethod.getResponseBodyAsString();System.out.println(res);}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {doGet();System.out.println(-----------分割线------------);System.out.println(-----------分割线------------);System.out.println(-----------分割线------------);doPost();}
}
CloseableHttpClient
使用httpclient.jarmaven依赖如下 dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.2/version/dependencyjava示例代码如下
package com.example.study.demo.http;import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;/*** CloseableHttpClient使用示例* jar包httpclient.jar*/
public class CloseableHttpClientTest {public static void doGet() {CloseableHttpClient client HttpClientBuilder.create().build();HttpGet get new HttpGet(http://www.thepaper.cn);try {// 很奇怪使用CloseableHttpClient来请求澎湃新闻的首页GTE请求也必须加上下面这个Header但是使用HTTPClient则不需要get.addHeader(User-Agent, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36);HttpResponse response client.execute(get);String res EntityUtils.toString(response.getEntity());System.out.println(res);} catch (IOException e) {e.printStackTrace();}}public static void doPost() {CloseableHttpClient client HttpClientBuilder.create().build();HttpPost post new HttpPost(https://www.thepaper.cn/www/commentPraise.msp);try {post.addHeader(User-Agent, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36);ListNameValuePair params new ArrayList();params.add(new BasicNameValuePair(commentId, 18718372));post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));HttpResponse response client.execute(post);String res EntityUtils.toString(response.getEntity());System.out.println(res);} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {doGet();System.out.println(-----------分割线------------);System.out.println(-----------分割线------------);System.out.println(-----------分割线------------);doPost();}
}