Java HTTPUrlConnection returns 500 status codes

I am trying to get the URL using HTTPUrlConnection, but I always get the 500 code, but when I try to get the same URL from the browser or use curl, it works fine!

This is the code

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

When I print the header fields, it shows the code 500. When I change the url to something else, for example google.com, it works fine. But I do not understand why it does not work here, but it works fine in the browser and with curl.

Any help would be greatly appreciated.

Thank,

+5
source share
7 answers

- . , 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"))
+7

"URL , http-get java, 500".

, http-get /default.aspx /login.aspx

        URL oUrl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) oUrl.openConnection();
        con.setRequestMethod("GET");
        ...
        int responseCode = con.getResponseCode();

: cookie, con.getResponseCode() . cookie :

header.key = null
     value = HTTP/1.1 302 Found
...
header.key = Location
     value = /default.aspx
header.key = Set-Cookie
     value = WebCom-lbal=qxmgueUmKZvx8zjxPftC/bHT/g/rUrJXyOoX3YKnYJxEHwILnR13ojZmkkocFI7ZzU0aX9pVtJ93yNg=; path=/
     value = USE_RESPONSIVE_GUI=1; expires=Wed, 17-Apr-2115 18:22:11 GMT; path=/
     value = ASP.NET_SessionId=bf0bxkfawdwfr10ipmvviq3d; path=/; HttpOnly
...

, , , : ! , . , ,...

redirect, "Set-cookie".

            con = (HttpURLConnection) oUrl.openConnection();
            con.setRequestMethod("GET");
            ...
            log.debug("Disable auto-redirect. We have to look at each redirect manually");
            con.setInstanceFollowRedirects(false);
            ....
            int responseCode = con.getResponseCode();

, responseCode:

private String getNewCookiesIfAny(String origCookies, HttpURLConnection con) {
    String result = null;
    String key;
    Set<Map.Entry<String, List<String>>> allHeaders = con.getHeaderFields().entrySet();
    for (Map.Entry<String, List<String>> header : allHeaders) {
        key = header.getKey();

        if (key != null && key.equalsIgnoreCase(HttpHeaders.SET_COOKIE)) {
            // get the cookie if need, for login
            List<String> values = header.getValue();
            for (String value : values) {
                if (result == null || result.isEmpty()) {
                    result = value;
                } else {
                    result = result + "; " + value;
                }
            }
        }
    }
    if (result == null) {
        log.debug("Reuse the original cookie");
        result = origCookies;
    }
    return result;
}
+2

, - ( ).

3xx, - -, 500 .

+1

, HTTP/1.1 500 ( Java) , , -.

, , , , Java ( @Muse).

, . - Crawlers.

0

, .

.

, , Rest Api , HTTP- 500 Android.

, Android, (Virtual Machine), , URL (IP).

. , .

0

, . , URLEncoder.encode(String, String)

0

All Articles