知识点

相关文章

更多

最近更新

更多

httpclient 带参数 get 请求

2019-03-16 22:56|来源: 网路

httpclient带参数get请求和默认get请求的主要区别是要初始化HttpGet的方式


默认的get请求初始化HttpGet的方式是new HttpGet("http://www.baidu.com/s?wd=java");


带参数get请求初始化HttpGet的方式如下:
先初始化URI对象
URI uri = new URIBuilder(" http://www.baidu.com/s").setParameter("wd", "java").build();
然后初始化HttpGet把初始化的URI对象传进去
new HttpGet(URI  url)


示例:

package com._656463.httpclient;
 
import java.net.URI;
 
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
/**
 * httpclient 带参数get请求
 */
public class DoGETParam {
    public static void main(String[] args) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 定义请求的参数
        URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd",
                "java").build();
        System.out.println(uri);
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(uri);
 
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为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();
        }
    }
}

本文整理于网络


相关问答

更多
  • 需要先判断一下是否登陆成功,只要登陆成功client对象就应该得到cookie了,你就可以用这个client对象直接请求你需要的页面。如果不用状态码判断登陆成功,状态码可能就没什么用了。
  • private static void post() throws IOException{ URL url = new URL(" http://www.baidu.com");// 提交地址 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true);// 打开写入属性 httpURLConnection.setDoInput(tru ...
  • import java.io.IOException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; impor ...
  • get 请求没有 请求体, 可以携带参数(数据)发送给服务器。 post 请求 有 头,有体,但是他的头不可以携带数据,数据只能放到 体里面。 大数据使用 post 请求,小数据传输 可以使用 get请求。 get 请求使用 不安全,携带的数据容易被人截取,所以 一般都使用 post请求。
  • 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 ...
  • String content = JSONBinder.binder(Vendor.class).toJSON(v); 不要转 string 直接发对象
  • 您正在使用的方法将第二个参数中的所有内容都用于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. ...
  • 我相信你误解了setAttribute / getAttribute方法的目的。 放入“getAttribute”检索请求的数据只能通过服务器上的setAttribute调用来设置。 客户端无法强制在那里设置值,因为将参数从客户端传递到服务器的唯一方法是通过参数(或POST请求中的某种其他结构)。 当使用RequestDispatcher时,getAttribute / setAttribute实际上用于在服务器代码之间传递信息。 I believe that you have misunderstood ...
  • 尝试使用此配置 HttpClient httpclient = new DefaultHttpClient(); HttpParams httpParameters = httpclient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParameters, WAIT_RESPONSE_TIME ...
  • 请求实体太大的错误,是由于服务器接收请求大于配置要处理的请求。 它不应该从客户端,你需要修改你的服务器设置,以允许更大的请求体长。 此参数将因服务器而异。 其中一些列在这里 Sorry the issue was not on client side. But the internal API that I am using was sending incorrect 413 response code instead of (almost) correct response code 507.