从HTTP POST请求500内部错误 [英] 500 Internal error from HTTP POST request

查看:366
本文介绍了从HTTP POST请求500内部错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用低于code上传使用HTTP POST文件,但我正在逐渐从服务器500内部服务器错误响应。

I am using below code to upload file using HTTP POST, but I am getting 500 Internal Server Error response from server.

能否请您看看,让我知道哪个code部分是罪魁祸首/失踪。还有就是在HTTPS连接没有错误,我觉得有些问题,在标头,这样服务器不接受此请求。

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.

    // 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");
    }

感谢

推荐答案

500内部服务器错误是服务器错误,IE浏览器。问题是在服务器端,而不是客户端。你需要检查服务器日志,看看是什么问题。

500 Internal Server Error is a server error, ie. the problem is on the server side, not client side. You need to check the server logs to see what the problem is.

标头是罚款。如果标题是错误的,你会得到400错误的请求或一些其他的4xx错误信息。

The headers are fine. If the headers were wrong, you would get 400 Bad Request or some other 4xx error instead.

这篇关于从HTTP POST请求500内部错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆