2016-09-21 55 views
1

我使用httplib的發送SOAP請求,但是當我把它我得到以下問題的工作與httplib的:如何使用cookie在python

INFO:root:Response:<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>401 Authorization Required</title> 
</head><body> 
<h1>Authorization Required</h1> 
<p>This server could not verify that you 
are authorized to access the document 
requested. Either you supplied the wrong 
credentials (e.g., bad password), or your 
browser doesn't understand how to supply 
the credentials required.</p> 
</body></html> 

我已經設置如下頭:

auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') 
headers = {"Content-type": "text/xml;charset=utf-8","SOAPAction":"\"\"","Authorization": "Basic %s" % auth} 

而不是python,如果我通過SoapUI發送,那麼它似乎是從服務器發送SoapUI的請求時,一些cookie正在發送,然後soapUI重新發送cookie。

回答

1

取代httplib,我使用了urllib2模塊。我不需要設置cookie,我更改了代碼以使用Digest類型驗證,而不是基本驗證:

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() 
password_mgr.add_password(None,uri=uri,user=username,passwd=password) 
authHandler=urllib2.HTTPDigestAuthHandler(password_mgr) 
opener = urllib2.build_opener(authHandler) 
response=opener.open(webServiceUrl2,data) 
res=response.read()