简体   繁体   中英

HttpURLConnection returns response code 500 after sending a GET request even though it works locally

I'm trying to send a HTTP GET request to download a file from the server as a part of a Selenium test case.

If I do it locally through any browser it works and returns HTTP OK 200, and the file is downloaded, but when I try to send a quest using HttpURLConnection class it does not.

Method I'm using:

    static sendGET(String URL){
        URL obj = new URL(URL)
        CookieHandler.setDefault(new CookieManager())
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication ("login", "password".toCharArray());
            }
        })
        HttpURLConnection con = (HttpURLConnection) obj.openConnection()
        HttpURLConnection.setFollowRedirects(true)
        con.setRequestMethod("GET")
        con.setRequestProperty("User-Agent", "Mozilla/5.0")
        int responseCode = con.getResponseCode()
        System.out.println("GET Response Code :: " + responseCode)
        return responseCode
    }

GET Response Code :: 500

From server logs I got:

CRITICAL 08:51:39   php     Call to a member function getId() on null

{
    "exception": {}
}

The line in which getId() is called: @AndiCover

$response = $transmitter->downloadFile($fileID, $this->getUser()->getId());

This makes is seem like a problem with user authentification.

I also tried using HttpGet class but the result was the same.

Figured out a problem, here it goes.

Well, what was the issue? Turned out my request lacked a Cookie header that could authenticate the user, and to be specific it was the PHPSESSID. To get the current PHPSESSID I created a method that retrieves all cookies and then substring PHPSESSID:

static getPhpSessionID(){
    String cookies = driver.manage().getCookies()
    System.out.println("Cookies: ${cookies}")
    cookies = cookies.substring(cookies.lastIndexOf("PHPSESSID=") + 10)
    cookies = cookies.substring(0, cookies.indexOf(";"))
    System.out.println("${cookies}")
    return cookies
}

First it prints all the cookies:

Cookies: [PHPSESSID=2ohpfb3jmhtddcgx1lidm5zwcs; path=/; domain=domain.com]

And then it substrings PHPSESSID:

2ohpfb3jmhtddcgx1lidm5zwcs

After that I needed to modify my sendGET method:

String sessionID = getPhpSessionID()
con.setRequestProperty("Cookie", "PHPSESSID=${sessionID}")

And the result was:

GET Response Code :: 200

Hope it helps someone in the future :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM