httpclient 基础教程
- httpclient依懒包官网下载及httpclient maven依懒包获取
- httpClient快速入门
- httpclient get请求
- httpclient 带参数 get 请求
- httpclient post 请求
- httpclient 模拟浏览器请求
- httpclient post 表单参数请求
- HttpClient PUT请求示例
- HttpClient DELETE请求示例
httpclient 进阶教程
知识点
相关文章
更多最近更新
更多httpclient post 表单参数请求
2019-03-16 23:33|来源: 网路
httpclient post带参数请求的步骤:
1、声明NameValuePair列表对象,把相关的参数添加到此列表中
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
2、初始化BasicNameValuePair对象,就是参数
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "java"));
3、构造一个form表单式的实体UrlEncodedFormEntity,并把List<NameValuePair>参数列表添加到该表单中
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
4、将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
示例:
package com._656463.httpclient; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class DoPOSTParam { public static void main(String[] args) throws Exception { // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建http POST请求 HttpPost httpPost = new HttpPost("http://www.oschina.net/search"); // 设置2个post参数,一个是scope、一个是q List<NameValuePair> parameters = new ArrayList<NameValuePair>(0); parameters.add(new BasicNameValuePair("scope", "project")); parameters.add(new BasicNameValuePair("q", "java")); // 构造一个form表单式的实体 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); // 将请求实体设置到httpPost对象中 httpPost.setEntity(formEntity); httpPost.setHeader( "User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"); CloseableHttpResponse response = null; try { // 执行请求 response = httpclient.execute(httpPost); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { String content = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println(content); } } finally { if (response != null) { response.close(); } httpclient.close(); } } }
本文参考网络
相关问答
更多-
java 使用HttpClient发送post请求的问题[2022-02-11]
Post请求是通过这种键值对参数的形式,添全URL那是get方式 -
如何使用httpclient进行post请求[2022-02-09]
http请求传递的都是字符串。这种情况一般使用xml为信息载体。把java类转成xml, client得到xml再转成java类。 UserDTO user = new UserDTO(); ...//填充user XStream xstream = new XStream();//使用xstream转换pojo和xml字符串 // HttpPost连接... -
httpclient发post请求加入队列和多线程来处理请求[2022-08-07]
首先new一个LinkedBlockingQueue,然后有三个方法相应的去处理,一个是将所有post请求加入对列,即initPost,另一个是从取post请求getUrl,还有一个解出新的url并将形成新的post请求放入队列addUrl。然后new 几个线程就可以进行了,每个线程各司其职。注意不要用普通队列来放post请求,因为在在多线程下它的线程不安全,而linkedBlockingQueue是concurrent类库的 ,线程安全。 -
httpclient post请求 怎么接收参数[2023-04-10]
HttpPost httpPost = new HttpPost(hturl); List formparams = new ArrayList (); formparams.add(new BasicNameValuePair("name", "admin")); formparams.add(new BasicNameValuePair("password", "123456")); UrlEncodedFormEntity uefEntity; uefEntity = new UrlEncodedFo ... -
httpclient post请求 怎么接收参数[2021-12-06]
String content = JSONBinder.binder(Vendor.class).toJSON(v); 不要转 string 直接发对象 -
httpclient post请求有大小限制吗[2023-07-17]
HttpPost httpPost = new HttpPost(hturl); List formparams = new ArrayList(); formparams.add(new BasicNameValuePair("name", "admin")); formparams.add(new BasicNameValuePair("password", "123456")); UrlEncodedFormEntity uefEntity; -
怎么用httpclient发送post请求[2021-11-29]
有时候我们在发送HTTP请求的时候会使用到POST方式,如果是传送普通的表单数据那将很方便,直接将参数到一个Key-value形式的Map 中即可。但是如果我们需要传送的参数是Json格式的,会稍微有点麻烦,我们可以使用HttpClient类库提供的功能来实现这个需求。假设我们需要发 送的数据是: { "blog": "http://www.iteblog.com", "Author": "iteblog" } 我们可以通过JSONObject够着Json: JSONObject jsonObject = ... -
HTTP Post请求使用HttpClient需要2秒,为什么?(HTTP Post requests using HttpClient take 2 seconds, why?)[2023-01-15]
好的,我自己解决了一些更多的调查。 我只需要添加一个将HTTP版本设置为1.1的参数,如下所示: HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpClient httpclient = new DefaultHttpClient(params); 我发现这是由于非常好的HttpHelper类从书呆子和一 ... -
using System; using System.Collections.Generic; using System.Net.Http; class Program { static void Main(string[] args) { Task.Run(() => MainAsync()); Console.ReadLine(); } static async Task MainAsync() { using ...
-
您正在使用的方法将第二个参数中的所有内容都用于HttpContent。 你可以在这里阅读更多。 你应该做的是将参数追加到myUri变量。 The method you are using takes everything in the 2nd parameter and uses it for HttpContent. You can read more here. What you should be doing is appending the parameters to myUri variable. ...