Java HTTPUrlConnection 返回 500 状态码

标签 java httpurlconnection

我正在尝试使用 HTTPUrlConnection 获取一个 url,但是我总是得到一个 500 代码,但是当我尝试从浏览器或使用 curl 访问相同的 url 时,它工作正常!

这是代码

try{
    URL url = new URL("theurl"); 
    HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
    httpcon.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    httpcon.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:14.0) Gecko/20100101 Firefox/14.0.1");
    System.out.println(httpcon.getHeaderFields());
    }catch (Exception e) {
        System.out.println("exception "+e);
    }

当我打印 headerfields 时,它显示 500 代码。当我将 URL 更改为其他内容(如 google.com )时,它工作正常。但我不明白为什么它在这里不起作用,但它在浏览器和 curl 上运行良好。

任何帮助将不胜感激..

谢谢,

最佳答案

这主要是由于编码而发生的。 如果您使用的浏览器正常,但在您的程序中出现 500(内部服务器错误),那是因为浏览器具有关于字符集和内容类型的高度复杂的代码。

这是我的代码,它适用于 ISO8859_1 作为字符集和英语的情况。

public void sendPost(String Url, String params) throws Exception {


    String url=Url;
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    con.setRequestProperty("Acceptcharset", "en-us");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    con.setRequestProperty("charset", "EN-US");
    con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    String urlParameters=params;
    // Send post request
    con.setDoOutput(true);
    con.setDoInput(true);
    con.connect();
    //con.

    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());
    this.response=response.toString();
    con.disconnect();

}

在主程序中,这样调用:

myclassname.sendPost("https://change.this2webaddress.desphilboy.com/websitealias/orwebpath/someaction","paramname="+URLEncoder.encode(urlparam,"ISO8859_1"))

关于Java HTTPUrlConnection 返回 500 状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11721821/

相关文章:

java - 带有参数的 HttpURLConnection GET 调用不起作用

java - Mockito框架

java - 在我的临时文件目标中添加奇怪的字符,这是一个错误吗?

java - 我可以使用 PUT 和 HttpUrlConnection 将图像发送到我的共享主机帐户吗?

android - 在 Android 中,为什么所有 org.apache.http.* 类都在 API 22 中被弃用(我应该使用什么作为替代品)?

java - HTTP 客户端 SSL 连接 setEnabledCipherSuites

java - 非法状态异常 : Already Connected while connecting to a HttpURL

java - 使用 pdfBox 的横向 PDF

java - 为什么 w3c.dom.Element 是按照字母顺序设置属性的?

java - 如何使用在编译时设置的动态数量的参数创建 java 方法(类似 lombok 的行为)