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;
}
Similar Posts:
- HttpURLConnection Cannot write output after reading input.
- [How to Solve] Unexpected end of file from server
- [Solved] Exception in thread “main” javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
- [How to Solve] Attempted to finish an input event but the
- System.Data.SqlClient.SqlCommand
- JavaWeb HttpServletRequest (How to Get Line, Header and Body)
- The differences between response. Getwriter(). Write() and response. Getwriter(). Print(), as well as pritwriter object and out object
- hessian http response code:411 [How to Solve]
- Python3 urlopen() TypeError: can’t convert ‘bytes’ object to str im…
- How to Solve Error: Request failed with status code 500