Selenium 2: Detect content type of link destinations

后端 未结 3 1961
猫巷女王i
猫巷女王i 2021-01-15 19:16

I am using the Selenium 2 Java API to interact with web pages. My question is: How can i detect the content type of link destinations?

Basically, this is the backgro

3条回答
  •  广开言路
    2021-01-15 19:51

    As Jochen suggests, the way to get the Content-type without also downloading the content is HTTP HEAD, and the selenium webdrivers does not seem to offer functionality like that. You'll have to find another library to help you with fetching the content type of an url.

    A Java library that can do this is Apache HttpComponents, especially HttpClient.

    (The following code is untested)

    HttpClient httpclient = new DefaultHttpClient();
    HttpHead httphead = new HttpHead("http://foo/bar");
    HttpResponse response = httpclient.execute(httphead);
    BasicHeader contenttypeheader = response.getFirstHeader("Content-Type");
    
    System.out.println(contenttypeheader);
    

    The project publishes JavaDoc for HttpClient, the documentation for the HttpClient interface contains a nice example.

提交回复
热议问题