I'm able to set the Accept-Language header , but somehow I'm unable to set the Connection header to "keep-alive":
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU'
webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Connection'] = "keep-alive"
driver = webdriver.PhantomJS("/home/user/bin/phantomjs",service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
driver.set_window_size(1120, 550)
driver.get("http://www.httpbin.org/headers")
print(driver.page_source)
Output:
<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "ru-RU",
"Host": "www.httpbin.org",
"User-Agent": "Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1"
}
}
</pre></body></html>
I thought maybe, for whatever reason, the header itself or the fields were case sensitive, so I looked up examples of those headers and used them exactly as is, but no dice. How do I set the Connection header or Keep-alive header?
Looks like the default header for phantomjs connection is Keep-alive, the site you are using to view headers does not show the Connection header, even when not using PhantomJS. If you look at your request using Fiddler, you can see that it has the connection keep-alive header
GET /headers HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1
Connection: Keep-Alive
Accept-Encoding: gzip, deflate Accept-Language: en-US,* Host: www.httpbin.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With