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+"¶m02="+ param02+"¶m03="+ param03+"¶m05="+ param05+"¶m08="+ param08+"¶m11="+ 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.
The server has crashed. The
connection.getErrorStream()may contain a detailed error report. You need to read it in thecatchblock.As to your code, at least the way how you URL encoded the query string is not right.
You should not URL encode the separator characters
&and=at all. Fix it as follows: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
DataOutputStreamto write the request body. It’s intented for creating so-called.datfiles where data blocks are formatted and separated in a special manner at binary level. Just write it straight to theconnection.getOutputStream().That the server has crashed instead of returning some HTTP
4nnerror 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: