2011-03-05 7 views
4

SOAP POST를 통해 API 호출을 시도하고 있는데 계속 입력합니다. "TypeError : 유효한 문자열이 아닌 시퀀스 또는 매핑 객체가 아닙니다." @ data = urllib.urlencode (값)urllib2를 사용하여 SOAP POST를 수행하지만 오류가 계속 발생합니다.

SM_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Header> 
    <AutotaskIntegrations xmlns="http://Autotask.net/ATWS/v1_5/"> 
     <PartnerID>partner id</PartnerID> 
    </AutotaskIntegrations> 
    </soap:Header> 
    <soap:Body> 
    <getThresholdAndUsageInfo xmlns="http://Autotask.net/ATWS/v1_5/"> 
    </getThresholdAndUsageInfo> 
    </soap:Body> 
</soap:Envelope>""" 

values = SM_TEMPLATE%() 
data = urllib.urlencode(values) 
req = urllib2.Request(site, data) 
response = urllib2.urlopen(req) 
the_page = response.read() 

어떤 도움을 주시면 감사하겠습니다.

답변

5

urllib.urlencode 기능 키 - 값 쌍의 시퀀스 또는 dict 같은 매핑 타입 기대 :

>>> urllib.urlencode([('a','1'), ('b','2'), ('b', '3')]) 
'a=1&b=2&b=3' 

는 SOAP의 HTTP의 POST를 수행하기를, 당신은있는 그대로 SM_TEMPLATE의 덩어리를두고로 설정해야합니다 POST 본문을 만든 다음 POST 본문의 인코딩 및 charset에 대한 Content-Type 헤더를 추가합니다. 예를 들어 :

data = SM_TEMPLATE 
headers = { 
    'Content-Type': 'application/soap+xml; charset=utf-8' 
    } 
req = urllib2.Request(site, data, headers) 
+0

큰 감사 :-) 주시면 감사하겠습니다 : 그것은 오라클 데이터 통합 ​​(오라클 ODI) .Obviously, 당신은 귀하의 경우에 맞게 살전 것들에 대한 값을 적응해야 호출 나를 위해 일한 ! 그 다음 오류로 수정, 롤 – George

+0

또한 'soapAction'을 추가해야합니다 : 'GetAllItem'내 요청을 작동 시키려면 – kelvan

0

는 예를 들어 아래의 코드를 참조하십시오, 당신이 파이썬 2.6.6에 대한 urllib2를 사용하여 SOAP 요청을 해결하는 데 도움이 될 수 있습니다.

import urllib2 

url = "http://alexmoleiro.com:20910/oraclediagent/OdiInvoke?wsdl" 

post_data = """<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> 
    <Body> 
     what_you_want_to_send_in_a_correct_format 
    </Body> 
</Envelope> 
""" 

http_headers = { 
    "Accept": "application/soap+xml,multipart/related,text/*", 
    "Cache-Control": "no-cache", 
    "Pragma": "no-cache", 
    "Content-Type": "text/xml; charset=utf-8" 

} 

request_object = urllib2.Request(url, post_data, http_headers) 

#DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES 
http_proxy_server = "10.1.2.3" 
http_proxy_port = "8080" 
http_proxy_realm = http_proxy_server 
http_proxy_full_auth_string = "http://%s:%s" % (http_proxy_server, http_proxy_port) 
proxy = urllib2.ProxyHandler({'http': http_proxy_full_auth_string}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 
#END OF --> DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES 

response = urllib2.urlopen(request_object) 
html_string = response.read() 
print html_string 

모든 의견이

관련 문제