Tag Archives: java.io.IOException: Server returned HTTP response code: 500 for URLxxxxxxxx

Server returned HTTP response code: 500 for URL xxxxxxxxxxxxx

Experience of stepping on the pit

Because the project needs to connect to other interfaces, it uses urlconnection post to request the HTTPS interface. When sending the JSON array, it encounters java.io.ioexception: server returned HTTP response code: 500 for URL

At that time, the local test passed and returned normally. If you put it on the Linux cloud server and passed the test and returned normally, there was a problem when you put it on the windows server. That’s what I said above

According to the error analysis, we first contact the receiver and find that the receiver has no error content, so we find the problem from ourselves. The first thing I think about is the coding format

Try 1

connection.setRequestProperty(“User-Agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)”);

It didn’t solve the problem in the end

Try 2

conn.setRequestProperty(“charsert”, “utf-8”);

It didn’t solve the problem in the end

Try 3

Exclude that the request parameter is empty

It didn’t solve the problem in the end

Try 4

out.println(param.getBytes(“UTF-8”));

It didn’t solve the problem in the end

Try 5

Use OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), “utf-8”);

out.write(param);

Substituted

PrintWriter out = new PrintWriter(conn.getOutputStream()); // Printwriter was used for packaging
out. Println (param)

The problem is solved!!!!!!!! Ten thousand grass mud horses gallop by & gt_& lt;

Finally paste the code, hope to help you

 /** post request */
 public static String reqPost(String url, String param) throws IOException {
  String res = "";
  URLConnection conn = getConnection(url); // POST requires that the URL does not contain request parameters
  conn.setDoOutput(true); // The two request properties must be set to true, which means that POST is used by default
  conn.setDoInput(true);
  // conn.setRequestProperty("charsert", "utf-8");
  // request parameters must be output to the request body parameters using the OutputStream obtained by conn
  // Wrapped with PrintWriter
  /* PrintWriter out = new PrintWriter(conn.getOutputStream());
  out.println(param.getBytes("UTF-8"));*/
  OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
  out.write(param);
  out.flush(); // immediately flush to request body) PrintWriter writes in memory cache first by default
  try// send normal request (get resources)
  {
   BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
   String line;
   while ((line = in.readLine()) != null) {
    res += line + "\n";
   }
  } catch (Exception e) {
   e.printStackTrace();
   log.error(e.toString());
  }
  return res;
 }