commons httpclient - Adding query string parameters to GET/POST request

前端 未结 6 2192
情深已故
情深已故 2020-12-04 23:38

I am using commons HttpClient to make an http call to a Spring servlet. I need to add a few parameters in the query string. So I do the following:

HttpReques         


        
6条回答
  •  余生分开走
    2020-12-05 00:14

    I am using httpclient 4.4.

    For solr query I used the following way and it worked.

    NameValuePair nv2 = new BasicNameValuePair("fq","(active:true) AND (category:Fruit OR category1:Vegetable)");
    nvPairList.add(nv2);
    NameValuePair nv3 = new BasicNameValuePair("wt","json");
    nvPairList.add(nv3);
    NameValuePair nv4 = new BasicNameValuePair("start","0");
    nvPairList.add(nv4);
    NameValuePair nv5 = new BasicNameValuePair("rows","10");
    nvPairList.add(nv5);
    
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    URI uri = new URIBuilder(request.getURI()).addParameters(nvPairList).build();
                request.setURI(uri);
    
    HttpResponse response = client.execute(request);    
    if (response.getStatusLine().getStatusCode() != 200) {
    
    }
    
    BufferedReader br = new BufferedReader(
                                 new InputStreamReader((response.getEntity().getContent())));
    
    String output;
    System.out.println("Output  .... ");
    String respStr = "";
    while ((output = br.readLine()) != null) {
        respStr = respStr + output;
        System.out.println(output);
    }
    

提交回复
热议问题