Wednesday, April 8, 2009

Http Client Usage

public String query(String host, int port, String queryString) throws SolrException {

HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost(host,port);
client.setHostConfiguration(hostConfig);

GetMethod method = new GetMethod(queryString);
String buffer;
StringBuilder response;
try {

int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {
log.error("Method failed: " + method.getStatusLine());
throw new HttpException(method.getStatusLine().toString());
}else{
BufferedReader in = new BufferedReader(new InputStreamReader
(method.getResponseBodyAsStream(), method.getResponseCharSet()));
response = new StringBuilder();
while( (buffer=in.readLine()) != null){
response.append(buffer);
}
}
} catch (HttpException e) {
log.error("Fatal protocol violation: ",e);
throw new SolrException(e);
} catch (IOException e) {
log.error("Fatal transport error: ", e);
throw new SolrException(e);
} finally {
method.releaseConnection();
}
return response.toString();
}

No comments:

Post a Comment