Search code examples
javahttprequestfiddler

I am having difficulty with Fiddler detecting HttpClient.execute(get) from a Java Application


I'm a programmer currently straddling the fence between .net and Java, but I lean more towards .net with limited experiance with java. I have a Java application, not developed by myself, that does an httpClient execute(get) (get being an HttpsFactory) to get a response back from asp classic code that lives on a different code server found through a specific url with a querystring to store user authentication info. Don't ask me why.

One of the users our qa team is testing with is not returning a good response so I am trying to use fiddler to see what we are getting back. I can see it is connecting because if I enter a malformed password which validated on the asp side. It generated the proper error message and we get it back. Fiddler is not detecting any of this. I've looked at the fiddler sight to try and configure my jvm to go through fiddlers proxy server. But I can't get this to work. I am running java version 1.7.0.21 which I beleive is the latest.

I've run the following in the command prompt

jre -DproxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 and jre -DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888 MyApp

and jre isn't recognized. I also tried:

java -DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888 MyApp and java -DproxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888

the properties are not recognized

I also tried

java -Dhttp.proxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888

and I just get the usage for the java command.

I have also tried embedding the following into my code just befoe the http connection and I am still getting no detection.

System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "8888");
System.setProperty("https.proxyPort", "8888");

Here java code I am running.

public String getHttpConnect(User user) {
    HttpClient client=https.getHttpClient();
    InputStream instream=null;
    HttpEntity entity=null;
    HttpGet get = https.getHttp(user);
    try {
            response = client.execute(get);
            entity = response.getEntity();
        if (entity != null) {
             instream = entity.getContent();
             Document document= read.read(instream);
            Element root = document.getRootElement();
            String content = root.elementText("permission");
            if (content.equals("OK")) {
                message = Global.CONNECT_SUCCESS;
            } else {
                message = ErrorMsg.CONNECT_FAIL;
            }
        }

    }catch (Exception e) {
        message = ErrorMsg.CONNECT_EXCEPTION;
        get.abort();
        log.error("HttpConnect error: "+e.toString());
    }finally{
        try {
            if(instream!=null)instream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Inputstream exception "+e.toString());
            log.error("Inputstream close exception: "+e.toString());
            e.printStackTrace();
            message = ErrorMsg.CONNECT_EXCEPTION;
        }
        client.getConnectionManager().shutdown();
    }
        return message;
}

public HttpClient getHttpClient() {
    HttpClient client = new DefaultHttpClient();
    client = WebClientDevWrapper.wrapClient(client);
    client.getParams().setParameter(
            CoreConnectionPNames.CONNECTION_TIMEOUT,
            Global.CONNECTION_TIMEOUT);// connect timeout 
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
            Global.RESPONSE_TIMEOUT);// send data read timeout 
    return client;
}

public HttpGet getHttp(User user) {
    String psw = user.getAccountpsw();
    StringBuffer connectUrl = new StringBuffer();
    if (Global.CURRENT_RUN_TYPE.equals(Global.BETA)) {
        connectUrl.append(Global.DEFAULT_LOGIN_URL_BETA);
    } else {
        connectUrl.append(Global.DEFAULT_LOGIN_URL_LIVE);
    }
    connectUrl.append("?usr=");
    connectUrl.append(user.getAccountname());
    connectUrl.append("&pwd=");
    connectUrl.append(psw);
    connectUrl.append("&vers=2.5.20&");
    connectUrl.append(random());
    // connectUrl.append("&SkipCheckForFiles=TRUE");
    StringBuffer logConnect = new StringBuffer();
    logConnect.append("Logging into server using URL:");
    logConnect.append(Global.LOG_NEWLINE);
    logConnect.append(Global.DES.getLogEncString(connectUrl.toString()));
    logConnect.append(Global.LOG_NEWLINE);
    log.info(logConnect);
    HttpGet get = new HttpGet(connectUrl.toString());
    return get;
}

Thanks for all of your help, regardless of the outcome.


Solution

  • When this sort of thing fails or is difficult to setup what you can do is setup Fiddler in reverse proxy mode.

    Basically, what you would do is in your java code, do your http get to some made up url on your local box with a specific port. Then in Fiddler, you can use a simple Fiddler script added inside the OnBeforeRequest method that looks something like this:

    //PROXY to different server example
    if(oSession.HostnameIs("http://localhost:8989/path/to/whatever/")){
         oSession.hostname = "http://real/path/to/whatever/on/the/internet";    
    }
    

    Here is more information on how to setup Fiddler in reverse proxy mode: http://fiddler2.com/documentation/Configure-Fiddler/Tasks/UseFiddlerAsReverseProxy

    This will effectively accomplish the same thing and allow you to view all traffic. The only downside being that you have to modify your java code temporarily to troubleshoot this issue. This method also works on any platform java, .net, php, etc...because you're simply just forcing your application network requests through Fiddler then redirecting it.

    I hope this was helpful.