help on HTTP 400 Bad Request syntax error on urllib2.urlopen

Jan 10, 2012
Reaction score
I have a piece of code where I log on to a web site - through a proxy server and using web site credentials. I log in just fine. Then I try to get into a page where I sent the session Id I got, the cookies seem
to be sent just fine but I get a HTTP 400 Bad Request. Please see the syntax of my request and let me know what I'm missing. I'd really appreciate any feedback on this!

Thanks a lot in advance,

import urllib, urllib2, cookielib
import requests
proxy_info = {
'user' : 'myuser',
'pass' : 'mypassword',
'host' : "",
'port' : 8080
# build a new opener that uses a proxy requiring authorization
proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)[email protected]%(host)s:%(port)d" % proxy_info})
cj = cookielib.CookieJar()
cookie_h = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler(debuglevel=1) , cookie_h)
headers={'User-agent' : 'Mozilla/5.0'}
# install it
url = '[email protected]&user_password=myapppassword'
f = urllib2.urlopen(url)
html =
print html
url2 = '[email protected]&sid=329479960 HTTP/1.1'
response = urllib2.urlopen (url2)
html2 =
print html2

I get this back:
send: 'GET[email protected]&sid=329479960 HTTP/1.1 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost:\r\nProxy-Authorization: Basic aWNhcnJlb246YWdqYTEZ\r\nCookie: ASPSESSIONIDAQBASTST=CGDGDKDBEDEAGJJOINKPFGCC\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Cache-Control: no-cache
header: Pragma: no-cache
header: Content-Type: text/html; charset=utf-8
header: Proxy-Connection: close
header: Connection: close
header: Content-Length: 730
Traceback (most recent call last):
File "C:\Aptana\myDev\root\nested\", line 45, in <module>
response = urllib2.urlopen (url2)
File "C:\PYTHON27\LIB\", line 126, in urlopen
return, data, timeout)
File "C:\PYTHON27\LIB\", line 400, in open
response = meth(req, response)
File "C:\PYTHON27\LIB\", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\PYTHON27\LIB\", line 438, in error
return self._call_chain(*args)
File "C:\PYTHON27\LIB\", line 372, in _call_chain
result = func(*args)
File "C:\PYTHON27\LIB\", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question