2

I have been dealing with a problem for some time and would really appreciate it if somebody can give me some pointers.

I basically have to perform an ajax call to an aspx page on another server. In IE8 this does not work due to cross-domain problems. Some people suggested I tried jquery's datatype "jsonp" and that doing so is going to allow cross-domain communication in IE8 but it failed.

So, in order to solve that problem, I have a browser detection routine in my code that checks if it is IE8. If it is IE8 what I do is that I do an ajax call to an interim jsp page.

What the interim jsp page is supposed to do, is take those parameters and basically post them to an aspx page.

Regarding this this is the code I am using:

<%

// get the parameters
String fileName = request.getParameter("fileName");
String param02 = request.getParameter("param02");
String param03 = request.getParameter("param03");
String param05 = request.getParameter("param05");
String param08 = request.getParameter("param08");
String param11 = request.getParameter("param11");


java.net.URL url;
java.net.HttpURLConnection connection = null;  
try {

    String urlParameters = "fileName="+ fileName+"&param02="+ param02+"&param03="+ param03+"&param05="+ param05+"&param08="+ param08+"&param11="+ param11;
    String encodedurl = java.net.URLEncoder.encode(urlParameters); 

    out.println(encodedurl);
    //Create connection
    url = new java.net.URL(
            "https://somepage.aspx");
    connection = (java.net.HttpURLConnection) url
            .openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    ////connection.setRequestProperty("Content-Type","text/xml; charset=utf-8");

    connection.setRequestProperty("Content-Length", "" + 
            Integer.toString(encodedurl.getBytes().length));
    connection.setRequestProperty("Content-Language", "en-US");  

    connection.setUseCaches (false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    //Send request
    DataOutputStream wr = new DataOutputStream (
            connection.getOutputStream ());
    wr.writeBytes (encodedurl);
    wr.flush ();
    wr.close ();
out.println(connection.getResponseCode());

    //Get Response

      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer responseBuffer = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        responseBuffer.append(line);
        responseBuffer.append('\r');
      }
      rd.close();
      out.println("\n\nRESPONSE\n\n" +responseBuffer.toString());
      out.println(connection.getErrorStream());



} catch (Exception e) {

    e.printStackTrace();
    out.println("Exception :(+ " +e);


} finally {

    if(connection != null) {
        connection.disconnect(); 
    }
}

    %>

The code results with the following error:

java.io.IOException: Server returned HTTP response code: 500 

When in chrome, the normal ajax call is performed and that works - nothing similar to this error code.

Can anybody please help out.

oneiros
  • 3,527
  • 12
  • 44
  • 71
  • As you're getting a HTTP response code of 500 (Internal Server Error), I'm guessing that IE 8 is transmitting your request in a manner that the aspx page isn't recognizing. Are you able to capture the actual HTTP request that's being sent out in IE and Chrome and compare them? – Zack Macomber Feb 22 '12 at 20:38

1 Answers1

1

The server has crashed. The connection.getErrorStream() may contain a detailed error report. You need to read it in the catch block.

As to your code, at least the way how you URL encoded the query string is not right.

String urlParameters = "fileName="+ fileName+"&param02="+ param02+"&param03="+ param03+"&param05="+ param05+"&param08="+ param08+"&param11="+ param11;
String encodedurl = java.net.URLEncoder.encode(urlParameters); 

You should not URL encode the separator characters & and = at all. Fix it as follows:

String charset = "UTF-8";
String urlParameters = String.format(
    "fileName=%s&param02=%s&param03=%s&param05=%s&param08=%s&param11=%s", 
        URLEncoder.encode(fileName, charset), 
        URLEncoder.encode(param02, charset), 
        URLEncoder.encode(param03, charset), 
        URLEncoder.encode(param05, charset), 
        URLEncoder.encode(param08, charset),
        URLEncoder.encode(param11, charset));

You should also be extremely careful with using the charset. You're relying on the platform default charset all the time instead of explicitly specifying it. The server may not necessarily use the same platform default charset. The content length for example may vary depending on the charset used.

You should also not be using the DataOutputStream to write the request body. It's intented for creating so-called .dat files where data blocks are formatted and separated in a special manner at binary level. Just write it straight to the connection.getOutputStream().

connection.getOutputStream().write(urlParameters.getBytes(charset));

That the server has crashed instead of returning some HTTP 4nn error in turn also indicates a programming bug in the server's code. If you have control over this, fix that as well. If not, report it to the responsible server admin.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • BalusC - that was exactly the problem. I encoded the parameters the way you specified and it worked. This is not the first time where you have helped me. I really thank you for that. – oneiros Feb 22 '12 at 20:58