2016-09-21 60 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()