Java HTTPPost请求返回HTTP错误500

时间:2019-02-19 12:06:53

标签: java json eclipse http http-status-code-500

private HttpPost getRequest() {
    HttpPost httpRequest = null;

    postParameter();

    String queryString = "";
    Set<Entry<String, String>> getPairs = this.parameterMap.entrySet();
    for (Entry<String, String> entry : getPairs) {
        try {
            queryString += URLEncoder.encode(entry.getKey(), this.CHARSET) + "=" + URLEncoder.encode(entry.getValue(), this.CHARSET) + "&";
        } catch (UnsupportedEncodingException e) {
        }
    }
    if (!queryString.isEmpty()) {
        queryString = "?" + queryString;
    }

    httpRequest = new HttpPost("http://mydomainname.com/java_test.php");
    String user_password = username+":"+password;
    byte[] Author = Base64.getEncoder().encode(user_password.getBytes());
    String Authorization = null;
    try {
        Authorization = new String(Author, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
    }
    String partnerID ="XXXXXX";     
    httpRequest.setHeader("Authorization",Authorization);
    httpRequest.setHeader("PartnerId",partnerID);       
    httpRequest.addHeader(HttpHeaders.CONTENT_TYPE,"application/json");
    httpRequest.addHeader(HttpHeaders.ACCEPT,"application/json");               
    List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
    Set<Entry<String, String>> postPairs = this.postParametersMap.entrySet();
    for(Entry<String, String> entry : postPairs) {
        BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(),
                entry.getValue());
        nameValuePairs.add(pair);
    }

    UrlEncodedFormEntity parameters = null;
    try {
        parameters = new UrlEncodedFormEntity(nameValuePairs, this.CHARSET);
    } catch (UnsupportedEncodingException e) {
    }

    httpRequest.setEntity(parameters);
    return httpRequest;
}

以上请求的HTTPS POST错误500,这是带有JSON数组的带有自定义标头(授权,伙伴ID,类型,接受)的简单HTTP客户。

在其他主机JAVA 7上相同的代码起作用,但在Java 11上未运行。

1 个答案:

答案 0 :(得分:1)

代码500是服务器错误。

请参见https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500

  

“超文本传输​​协议(HTTP)500内部服务器错误”服务器错误响应代码表示服务器遇到意外状况,阻止其满足请求。

在Java方面没有问题。