0

I am trying to make a post request to a https address and set up fiddler to return a standard response. I have two rules set up in Fiddler and the process works from both Internet Explorer and Postman (but not Chrome) and I cannot get it to work from the java application I am trying to write even when I have created an executeable jar file and run from the cmd. I have been using this example as the base for this work. I have the sendGet() working (ish) but I cannot get sendPost() to work getting a java.net.UnknownHostException.

I think the problem may be that I am not hitting Fiddler as the proxy from Eclipse. For the sendGet() from browser and Postman I get the contents of 200_SimpleHTML.dat as required but from eclipse the same rule has no affect and I get the content from the actual URL (Our TeamForge in this case)

My organisation uses a proxy which is set in IE and I have set the java configuration to "Use browser settings" and also tried "Use automatic proxy configuration script" (pointing to the proxy.pac file) and neither seems to have any affect. I have the following in Window -> Preferences -> Network Connections:

enter image description here

but I have no idea how, or even if, I can point to Fiddler as the proxy here. I am not setting up any authentication from the working routes.

The current state of my sendPost is below:

USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"

which I copied from Fiddler after one of the successful request.

private void sendPost() throws Exception
{
    String url = "<Actual URL removed>";
    URL obj = new URL(url);
    HttpURLConnection http = (HttpURLConnection) obj.openConnection();

    // add request header
    http.setRequestMethod("POST");
    http.setDoOutput(true); 
    http.setRequestProperty("User-Agent", USER_AGENT);
    http.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");

    OutputStream out = http.getOutputStream();

    int responseCode = http.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null)
    {
        response.append(inputLine);
    }
    in.close();

    // print result
    System.out.println(response.toString());
}

Does anyone have any ideas as to how I can get this to work from my Java app?

onesixtyfourth
  • 744
  • 9
  • 30
  • 1
    These proxy preferences are for Eclipse, not for Java application you run from within Eclipse. Does adding the following VM arguments to your run/launch configuration fix your problem? `-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888` – howlger Dec 05 '17 at 10:35
  • I haven't added them that way but I have just realised that I can add a Proxy object to the openConnection() method. This has worked for the get method but not the post. Error changed to java.net.socketException so this is definitely the area. Thanks – onesixtyfourth Dec 05 '17 at 10:45
  • Getting this now: fiddler.network.https> HTTPS handshake to (for #3) failed. System.IO.IOException The handshake failed due to an unexpected packet format. Failing to handshake with the proxy means that my autoresponder is now not catching the request I think. – onesixtyfourth Dec 05 '17 at 11:44
  • It would be easier not to use HTTPS. Could you use HTTP for testing with Fiddler instead? – howlger Dec 05 '17 at 12:34
  • Unfortunately not the 's' is the reason I need to do this. I have to be able to send to a https URL and catch it with Fiddler. Looking into the exception I got I think my autoresponder is not catching the request as the IOException is with the proxy of the system, which I am trying to circumvent for this call. :( – onesixtyfourth Dec 05 '17 at 12:40
  • Think I need to add some keys. – onesixtyfourth Dec 05 '17 at 13:37
  • Ok it seems that I have got as far with this as I need to. I have used the SSLTool from https://stackoverflow.com/questions/875467/java-client-certificates-over-https-ssl it is only for a test environment so should be ok. – onesixtyfourth Dec 05 '17 at 14:31

0 Answers0