irfan shafi irfan shafi - 2 months ago 20
HTTP Question

HTTP URL Connection Timeout

When I hit the following URL in browser it's working perfectly:

http://62.215.226.164/fccsms_P.aspx?UID=something&P=something&S=InfoText&G=96567771404&M=hello&L=E


but when I try to hit the URL with the following java code it's not working:

try {
URL url = new URL(null, "http://62.215.226.164/fccsms_P.aspx?", new sun.net.www.protocol.https.Handler());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

con.setRequestMethod("POST");

String urlParameters="UID=something&P=something=InfoText&G=96567771404&M=hello&L=E";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

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

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

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
catch(Exception e) {
System.err.println("Error: "+e.getMessage());
}


Is there anything wrong in the code?

Answer

You are sending a HTTP url through HttpsURLConnection. So its making problem. just change your first 2 line. You will get the proper output.

URL url = new URL("http://62.215.226.164/fccsms_P.aspx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
Comments