Issues performing a query to Solr via HttpClient u

2019-08-30 07:25发布

问题:

I am trying to query my local Solr server using HttpClient and I cannot figure out why the parameters are not being added to the GET call.

My code for doing this is:

HttpRequestBase request = new HttpGet("http://localhost:8080/solr/select");

HttpParams params = new BasicHttpParams();
params.setParameter("q", query);
params.setParameter("start", String.valueOf(start));
params.setParameter("rows", String.valueOf(rows));

request.setParams(params);

HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);

HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

return stringToStreamConversion(is); //500 error, NullPointerException, response is empty

I have tried to return several things in hopes of seeing what I would get and trying to figure out where the problem was. I have finally realized that I was only getting back the http://localhost:8080/solr/select when I returned

return request.getURI().toURL().toString();

I cannot figure out why the parameters are not getting added. If I do

return request.getQuery();

I get nothing back...any ideas? Thanks for the help in advance!

回答1:

From what I have seen you are not able to associate your paeans with the request.

So, instead of creating a new HttpParams object and associating it with request, can you try the following approach ?

httpCclient.getParams().setParameter("q", query"); ....



回答2:

The simpler option is to use the approach I used in HTTPPostScheduler, like this:

URL url = new URL(completeUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestMethod("POST");
conn.setRequestProperty("type", "submit");
conn.setDoOutput(true);

// Send HTTP POST
conn.connect();