Need help with HttpClient!
I'm trying to pull data from an our vendor
using HTTPClient's POST method. SOMETIMES (rather rarely) the thing
works, mostly not. Is something really wrong with the server or
my code is wrong? I posted 5 requests but only got 3 successfully responses. The other 2 returned some html tags which is not corect. The URLs are accessible works fine under my IE browser.
I really appreciate any help. Thanks.
Henry.
Here is my code:
import java.io.*;
import java.net.ConnectException;
import java.net.MalformedURLException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import sun.misc.BASE64Encoder;
public class URLHandlerTest {
private static String HOST = "http://test.marketwatch.com";
private static String HOST_SUFFIX = "/custom/wamu-com/xml-news.asp";
private static String PROXY = "10.53.206.199";
private static int PROXY_PORT = 8080;
private static String USERNAME = "svc_dbroker";
private static String PASSWORD = "Wephape6a8uq";
public static String getXMLfromMarketWatch(String queryParams) throws IOException {
StringBuffer url = new StringBuffer();
url.append(HOST);
url.append(HOST_SUFFIX);
if( queryParams != null ){
url.append("?");
url.append(queryParams);
}
String response = null;
response = getResponse(url.toString(),60,60);
return response;
}
public static String getResponse(String url, int responseTimeoutSec, int connectTimeoutSec)
throws IOException{
String response = null;
int status = -1;
try {
// Prepare HTTP get
GetMethod get = new GetMethod(url);
// Get HTTP client
HttpClient httpClient = new HttpClient();
// set timeouts
httpClient.setTimeout(1000 * responseTimeoutSec);
httpClient.setConnectionTimeout(1000 * connectTimeoutSec);
// make sure we set proxy
httpClient.getHostConfiguration().setProxy(PROXY, PROXY_PORT);
String encodedUserPwd = (new BASE64Encoder()).encode((USERNAME + ":" + PASSWORD).getBytes());
get.setRequestHeader("Proxy-Authorization", "Basic " + encodedUserPwd);
//Logger.debug(URLHandler.class.getName(), "Sending HTTP GET to: " + url);
// Execute request, which returns HTTP response code.
status = httpClient.executeMethod(get);
response = get.getResponseBodyAsString();
// store the response information in an object that can
// be used by client classes
get.releaseConnection();
return response;
} catch (ConnectException e) {
throw e;
} catch (MalformedURLException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
public static void main(String []args){
URLHandlerTest urlTest = new URLHandlerTest();
String []params = new String[5];
params[0] = "property=column&propValue=Real%20Estate%20Weekly&count=5";
params[1] = "property=column&propValue=Bond%20Report&count=5";
params[2] = "property=column&propValue=Economic%20Report&count=5";
params[3] = "property=column&propValue=Mortgages&count=5";
params[4] = "property=column&propValue=Real%20Estate%20Weekly&count=5";
try {
for(int i=0; i<params.length; i++){
String xmlResponse = urlTest.getXMLfromMarketWatch(params);
System.out.println("The xml return is: " + xmlResponse);
}
}
catch (IOException io){
io.printStackTrace();
}
}
}>

