Set user-agent property in https connection header

前端 未结 2 1074
清歌不尽
清歌不尽 2021-01-11 13:51

I am unable to properly set the user-agent property for an https connection. From what I\'ve gathered, http-header properties can be set through either the

2条回答
  •  春和景丽
    2021-01-11 13:58

    I've found/verified the problem by inspecting http communictions using WireShark. Is there any way around this?

    There is no problem here. The User-Agent header is set whether the request is transported via HTTP / HTTPS. Even setting it to something unreasonable like blah blah works on HTTPS. The headers shown below were captured when the underlying protocol used was HTTPS.

    Request headers sent via HTTPS

    User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive
    

    User-Agent: blah blah
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive
    

    Here's the code that triggers the request.

            // localhost:52999 is a reverse proxy to xxx:443
            java.net.URL url = new java.net.URL( "https://localhost:52999/" );
            java.net.URLConnection conn = url.openConnection();
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
            conn.connect();
            java.io.BufferedReader serverResponse = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
            System.out.println(serverResponse.readLine());
            serverResponse.close();
    

    Normally, HTTPS requests cannot be sniffed (like @Perception mentioned). Piping the request through a proxy that replaces the root CA with its own fake CA will allow you to see the traffic. A simpler method is to just look at the access log of the target server. But as you can see from the HTTPS request snippet above, the User-Agent header that is sent is correct.

提交回复
热议问题