2012-01-10 70 views
0

我有一段代碼,我登錄到一個網站 - 通過代理服務器並使用網站憑據。我登錄很好。然後我嘗試進入一個頁面,在那裏我發送了會話Id,Cookie看起來好像是 ,但是我收到了HTTP 400 Bad Request。請參閱我的請求的語法,並讓我知道我錯過了什麼。我真的很感激任何反饋!HTTP 400上的語法幫助urllib2.urlopen上的錯誤請求響應

非常感謝提前, 伊戈爾

import urllib, urllib2, cookielib 
proxy_info = { 
'user' : 'myuser', 
'pass' : 'mypassword', 
'host' : "myproxy.company.com", 
'port' : 8080 
} 

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'}  

urllib2.install_opener(opener)  
url = 'http://www.targetsite.com/LogIn.asp?user_id=&user_p assword=myapppassword' 
f = urllib2.urlopen(url) 
html = f.read() 
print html 
url2 = 'http://www.targetsite.com/Main.asp?uid=&sid=3294799 60 HTTP/1.1' 
response = urllib2.urlopen (url2) 
html2 = response.read() 
print html2 

我得到這個回:

send: 'GET http://www.targetsite.com/Main.asp?u...&sid=329479960 HTTP/1.1 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.targetsite.com\r\nProxy-Authorization: Basic aWNhcnJlb246YWdqYTEZ\r\nCookie: ASPSESSIONIDAQBASTST=CGDGDKDBEDEAGJJOINKPFGCC\r\nC onnection: 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\LaunchApp.py", line 45, in <module> 
response = urllib2.urlopen (url2) 
File "C:\PYTHON27\LIB\urllib2.py", line 126, in urlopen 
return _opener.open(url, data, timeout) 
File "C:\PYTHON27\LIB\urllib2.py", line 400, in open 
response = meth(req, response) 
File "C:\PYTHON27\LIB\urllib2.py", line 513, in http_response 
'http', request, response, code, msg, hdrs) 
File "C:\PYTHON27\LIB\urllib2.py", line 438, in error 
return self._call_chain(*args) 
File "C:\PYTHON27\LIB\urllib2.py", line 372, in _call_chain 
result = func(*args) 
File "C:\PYTHON27\LIB\urllib2.py", line 521, in http_error_default 
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 400: Bad Request 

回答

0

urlurl2是無效的。嘗試:

url = 'http://www.targetsite.com/LogIn.asp?' + urllib.urlencode(
    {"user_id": "", "user_p assword": "myapppassword"}) 
url2 = 'http://www.targetsite.com/Main.asp?' + urllib.urlencode(
    {"uid": "", "sid": "3294799 60 HTTP/1.1"}) 

名稱user_p assword不應可能包含空格。此外,sid的值似乎是可疑的。這可能是一個複製粘貼錯誤。

0

你爲什麼要在第二個URL中發送「HTTP/1.1」?該語法似乎不是有效的,ulrlib2無論如何都會發送HTTP/1.1。從痕跡來看,這看起來是問題所在。