| 21974 |
ashik.ali |
1 |
package in.shop2020.dtrapi.utils;
|
|
|
2 |
|
|
|
3 |
import java.io.BufferedReader;
|
|
|
4 |
import java.io.InputStream;
|
|
|
5 |
import java.io.InputStreamReader;
|
|
|
6 |
import java.net.URI;
|
|
|
7 |
import java.net.URL;
|
|
|
8 |
|
|
|
9 |
import org.apache.http.HttpHost;
|
|
|
10 |
import org.apache.http.HttpResponse;
|
|
|
11 |
import org.apache.http.HttpStatus;
|
|
|
12 |
import org.apache.http.client.methods.HttpGet;
|
|
|
13 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
14 |
import org.apache.http.params.CoreConnectionPNames;
|
|
|
15 |
|
|
|
16 |
public class HttpClientUtil {
|
|
|
17 |
public static String doGet(String hostName, int fofoId) throws Exception{
|
|
|
18 |
URI uri = new URL("http://"+hostName).toURI();
|
|
|
19 |
HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
|
|
|
20 |
DefaultHttpClient httpClient = new DefaultHttpClient();
|
|
|
21 |
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
|
|
|
22 |
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
|
|
|
23 |
HttpGet message = new HttpGet("/fofo/"+fofoId+"/edit");
|
|
|
24 |
HttpResponse response = httpClient.execute(host, message);
|
|
|
25 |
String responseString = toString(response.getEntity().getContent());
|
|
|
26 |
if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
|
|
|
27 |
String errorMessage = String.format("Could not process, responceCode = %d, responseString = %s", response.getStatusLine().getStatusCode(), responseString);
|
|
|
28 |
throw new Exception(errorMessage);
|
|
|
29 |
}
|
|
|
30 |
return responseString;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
private static String toString(InputStream inputStream) throws Exception{
|
|
|
34 |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
35 |
StringBuilder responseString = new StringBuilder();
|
|
|
36 |
String line = null;
|
|
|
37 |
while((line = bufferedReader.readLine()) != null){
|
|
|
38 |
responseString.append(line);
|
|
|
39 |
}
|
|
|
40 |
return responseString.toString();
|
|
|
41 |
}
|
|
|
42 |
}
|