简体   繁体   English

500 来自 HTTP POST 请求的内部错误

[英]500 Internal error from HTTP POST request

I am using below code to upload file using HTTP POST, but I am getting 500 Internal Server Error response from server.我正在使用以下代码使用 HTTP POST 上传文件,但我从服务器收到 500 Internal Server Error 响应。

Can you please have a look and let me know which code part is culprit/missing.能否请您看一下,让我知道哪个代码部分是罪魁祸首/缺失。 There is no error in HTTPS connection, I think some problem in Header so server is not accepting this request. HTTPS 连接没有错误,我认为 Header 有问题,所以服务器不接受这个请求。

    // Check server address
    url = new URL("https://example.com");
    String protocol = url.getProtocol(); 
    String host = url.getHost();
    String serviceRoot = url.getPath();

    // Build POST request
    HttpPost post = new HttpPost(new URI(protocol + "://" + host
            + serviceRoot));
    post.addHeader("User-Agent", "Test");
    post.addHeader("Content-type", "multipart/form-data"); 
    post.addHeader("Accept", "image/jpg"); 
    String authValue = "Basic "
            + Base64
                    .encodeBase64ToString(("username" + ":"
                            + "password").getBytes()) + " " + "realm=\"example.com\"";
    if (authValue != null) {
        post.addHeader("Authorization", authValue);
    }

    File file = new File("/sdcard/Download/IMAG0306.jpg");
    FileBody data = new FileBody(file);

    String file_type = "jpg" ;
    String description = "Test";

    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("file_name", new StringBody( file.getName() ) );
    reqEntity.addPart("description", new StringBody(description));
    reqEntity.addPart("file_type", new StringBody(file_type));
    reqEntity.addPart("data", data);

    post.setEntity(reqEntity); 

    if (true) {
        String trace = ">>> Send HTTP request:";
        trace += "\n " + post.getMethod() + " "
                + post.getRequestLine().getUri();
        System.out.println(trace);
    }

    if (true) {
        String trace = "<<< Send HTTP request-->:";
        trace += "\n" + post.toString();
        Header[] headers = post.getAllHeaders();
        for (Header header : headers) {
            trace += "\n" + header.getName() + " " + header.getValue();
        }
        System.out.println(trace);
    }

    HttpClient httpClient = createHttpClient();
    // replace with your url
    // “Authorization”, “Basic ” + encodedUsernamePassword);
    if (httpClient != null) {
        response = httpClient.execute(post);
        if (true) {
            String trace = "<<< Receive HTTP response:";
            trace += "\n" + response.getStatusLine().toString();
            Header[] headers = response.getAllHeaders();
            for (Header header : headers) {
                trace += "\n" + header.getName() + " " + header.getValue();
            }
            System.out.println(trace);
        }
    } else {
        throw new IOException("HTTP client not found");
    }

Thanks谢谢

500 Internal Server Error is a server error, ie. 500 Internal Server Error 是服务器错误,即。 the problem is on the server side, not client side.问题出在服务器端,而不是客户端。 You need to check the server logs to see what the problem is.您需要检查服务器日志以查看问题所在。

The headers are fine.标题很好。 If the headers were wrong, you would get 400 Bad Request or some other 4xx error instead.如果标头错误,您将收到 400 Bad Request 或其他一些 4xx 错误。

Check the server logs, especially if the endpoint is not being hit.检查服务器日志,尤其是在未命中端点的情况下。 In my case the error was: The request matched multiple endpoints.在我的例子中,错误是:请求匹配多个端点。 I had put other functions in the controller, which it thought were endpoints, but were not.我在 controller 中放置了其他函数,它认为这是端点,但实际上不是。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 改装后请求:500个内部服务器错误 - Retrofit post request: 500 internal server error 发送 Post 请求抛出 500 内部错误 - Sending a Post Request Throw 500 Internal error 静态Web服务:HTTP状态500。服务器遇到内部错误,阻止其满足此请求错误 - restful web services: HTTP Status 500.The server encountered an internal error that prevented it from fulfilling this request error HTTP 状态 500 – 内部服务器错误 – 服务器遇到意外情况,无法完成请求 - HTTP Status 500 – Internal Server Error - The server encountered an unexpected condition that prevented it from fulfilling the request 我无法发送 HTTP 请求(500 内部服务器错误) - I cannot send an HTTP request (500 internal server error) 500:发送 Post 请求时发生内部服务器错误 - 500:Internal Server Error while sending Post Request 基于简单 REST 的程序中的 HTTP 500 内部服务器错误。 从服务器接收/发送响应时对 GET 和 POST 感到困惑 - HTTP 500 Internal Server Error in simple REST based program. Confused in GET and POST while receiving/sending response from server 无法添加图片http.post POST 500内部服务器错误。 AngularJS Java Spring - Can't add Image http.post POST 500 internal server error. AngularJS Java Spring httpClient-&gt; HTTP / 1.1 500内部服务器错误 - httpClient -> HTTP/1.1 500 Internal Server Error App Engine中的HTTP 500内部服务器错误 - HTTP 500 Internal Server Error In App Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM