使用Python访问SOAP服务的问题
我正在尝试访问NJTransiteAPI,我已经使用Postman应用程序成功地查询了它,但是无论我尝试什么,我都无法让python成功地返回所需的查询。 使用肥皂水:使用Python访问SOAP服务的问题,python,soap,python-requests,Python,Soap,Python Requests,我正在尝试访问NJTransiteAPI,我已经使用Postman应用程序成功地查询了它,但是无论我尝试什么,我都无法让python成功地返回所需的查询。 使用肥皂水: from suds.client import Client url = "http://traindata.njtransit.com:8090/NJTTrainData.asmx?wsdl" client = Client(url, cache = None) from suds.sax.element import Ele
from suds.client import Client
url = "http://traindata.njtransit.com:8090/NJTTrainData.asmx?wsdl"
client = Client(url, cache = None)
from suds.sax.element import Element
auth = Element('UserCredentials')
auth.append(Element('userName').setText(my_username))
auth.append(Element('password').setText(my_password))
client.set_options(soapheaders = auth)
client = Client(url, cache = None)
result = client.service.getTrainScheduleJSON("NY")
这不会导致任何结果
我还尝试使用邮递员应用程序建议的预格式化请求,但是我一直收到404错误
import requests
url = "http://traindata.njtransit.com:8090/NJTTrainData.asmx"
querystring = {"wsdl":""}
payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Header>\n <UserCredentials xmlns=\"http://microsoft.com/webservices/\">\n <userName>---</userName>\n <password>---</password>\n </UserCredentials>\n </soap:Header>\n <soap:Body>\n <getTrainScheduleJSON xmlns=\"http://microsoft.com/webservices/\">\n <station>NY</station>\n </getTrainScheduleJSON>\n </soap:Body>\n</soap:Envelope>"
headers = {
'content-type': "text/xml; charset=utf-8",
'host': "traindata.njtransit.com",
'soapaction': "http//microsoft.com/webservices/getTrainScheduleJSON"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
我非常感谢您的帮助/见解。如果您不想设置主机头,这将由请求完成 404由错误的SOAPAction触发。缺少一个:http之后 下面的代码片段对我来说很好
import requests
url = "http://traindata.njtransit.com:8090/NJTTrainData.asmx"
querystring = {"wsdl":""}
payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Header>\n <UserCredentials xmlns=\"http://microsoft.com/webservices/\">\n <userName>---</userName>\n <password>---</password>\n </UserCredentials>\n </soap:Header>\n <soap:Body>\n <getTrainScheduleJSON xmlns=\"http://microsoft.com/webservices/\">\n <station>NY</station>\n </getTrainScheduleJSON>\n </soap:Body>\n</soap:Envelope>"
headers = {
'content-type': "text/xml; charset=utf-8",
'soapaction': 'http://microsoft.com/webservices/getTrainScheduleJSON'
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print response
print(response.text)