Web services 尝试使用ironpython和soap调用web服务

Web services 尝试使用ironpython和soap调用web服务,web-services,soap,wsdl,ironpython,Web Services,Soap,Wsdl,Ironpython,我正在尝试使用wsdl文件向Web服务器发送同步soap消息。根据IronPython中的操作说明,我为wsdl文件创建了一个cs文件和dll,以便在IronPython中加载。我已经创建了我的soap消息(webservice需要),但我认为我没有正确地构造我的webRequest 这是我的密码: import clr clr.AddReference("System") clr.AddReferenceToFileAndPath("C:\\Temp\MeetingSpaceServi

我正在尝试使用wsdl文件向Web服务器发送同步soap消息。根据IronPython中的操作说明,我为wsdl文件创建了一个cs文件和dll,以便在IronPython中加载。我已经创建了我的soap消息(webservice需要),但我认为我没有正确地构造我的webRequest

这是我的密码:

    import clr
clr.AddReference("System")
clr.AddReferenceToFileAndPath("C:\\Temp\MeetingSpaceService.dll")
clr.AddReference("System.Xml")
clr.AddReference("System.Web.Services")

from MeetingSpaceService import MeetingSpaceService
service = MeetingSpaceService()
from System.Net import WebClient
from System.IO import StreamWriter
from System.IO import StreamReader
from  System.IO import StringWriter
import System.Xml
import System.Text
from System import Uri
from System.Xml import XmlTextWriter
from System.Xml import Formatting
from System.Web.Services import *
from System.Net import HttpWebRequest
import System.Object
import System.Array
import System.Byte
from System.Xml import XmlDocument
from System.IO import File
from System.Object import ToString
#from System.ServiceModel.Description import WsdlImporter as wsdl 
from System.Web.Services.Description import (ServiceDescription,     ServiceDescriptionImporter)
from System.Web.Services.Protocols import SoapHttpClientProtocol
from System.IO import MemoryStream
from System.Net import WebClient

readerboard = ''' <?xml version="1.0" encoding="utf-8"?>
</soap:Envelope> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soap:Header>
  <wsa:Action>http://htng.org/PWSWG/2007/01/DigitalSignage#MeetingSpaceRequest</wsa:Action>
  <wsa:MessageID>urn:uuid:44088898-6926-4537-b1a3-4acc8e9f71d0</wsa:MessageID>
  <wsa:ReplyTo>
     <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
  </wsa:ReplyTo>
  <wsa:To>http://localhost:2025/MeetingSpaceProviderService/Request.asmx</wsa:To>
  <wsse:Security soap:mustUnderstand="1">
     <wsu:Timestamp wsu:Id="Timestamp-e157d58f-6589-4b3e-b2a9-5510dd9f270f">
        <wsu:Created>2011-04-17T15:35:25Z</wsu:Created>
        <wsu:Expires>2012-07-17T15:40:25Z</wsu:Expires>
     </wsu:Timestamp>"
     <wsse:UsernameToken wsu:Id="SecurityToken-843aeb4f-07de-4c78-a438-e051d3baa764">
        <wsse:Username>isacrb</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">is@cr6_662</wsse:Password>
     </wsse:UsernameToken>
  </wsse:Security>
我在这里做了一些改变,现在我得到的就是这些。web服务是不稳定的,所以我认为正在发生的是,当您发送请求时,响应会自动返回。在

rqt=webRequest.GetRequestStream() 在读取下一行之前,大约有1.5到2.5秒的延迟。这与SOAPUi中的行为类似,消息发送后通常会有一秒左右的延迟,然后返回的XML数据会显示在响应框中。我在想,在rqt=webRequest之后,我需要以某种方式读取流,而不需要请求响应。我可以向您发送我为导入WSDL而创建的cs文件和dll,看看您的想法吗。在这一点上,您应该能够访问服务器,并在IDE中看到我所说的内容。我感谢你的帮助。非常感谢你

以下是在上述操作之后发生的情况:

>>> rqt = webRequest.GetRequestStream()
>>> rqt.Write(byte1,0,byte1.Length)
>>>
>>>     #get the response here
>>> rsp = webRequest.GetResponse()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: The remote server returned an error: (500) Internal Server Error.
>>>     #create a stream reader to convert the raw response into UTF8 for text
>>> readStream = StreamReader(rsp.GetResponseStream(), System.Text.Encoding.UTF8
)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'rsp' is not defined
>>>     #read the data from the stream into a string
>>> data = readStream.ReadToEnd()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'readStream' is not defined
>>> readStream.Close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'readStream' is not defined
>>> rsp.Close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'rsp' is not defined
>>>     #Do stuff with the data here
>>> print data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'data' is not defined
>>>
rqt=webRequest.GetRequestStream() >>>rqt.Write(字节1,0,字节1.Length) >>> >>>#在这里获得回应 >>>rsp=webRequest.GetResponse() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 SystemError:远程服务器返回错误:(500)内部服务器错误。 >>>#创建流读取器,将原始响应转换为文本的UTF8 >>>readStream=StreamReader(rsp.GetResponseStream(),System.Text.Encoding.UTF8 ) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 NameError:未定义名称“rsp” >>>#将数据流中的数据读入字符串 >>>data=readStream.ReadToEnd() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 NameError:未定义名称“readStream” >>>readStream.Close() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 NameError:未定义名称“readStream” >>>rsp.Close() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 NameError:未定义名称“rsp” >>>#处理这里的数据 >>>打印数据 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 NameError:未定义名称“数据” >>>
当您发出指示SOAPAction的请求时,需要添加一个标题

比如:

webRequest.Headers.Add('SOAPAction', '"http://myservice.com/1/poll"') 
下面是调用Web服务的完整代码。在这种情况下

导入clr
从System.Net导入WebClient
从System.IO导入StreamReader
导入系统文本
从系统导入Uri
从System.Net导入HttpWebRequest
#这是SOAP负载
#只需要用基本的concats构建它
ReadBoard=“”
10011
"""
uriString=”http://wsf.cdyne.com/WeatherWS/Weather.asmx"
uri=uri(uri字符串)
尝试:
webRequest=HttpWebRequest.Create(uri)
webRequest.ContentType=“text/xml;charset=\”utf-8“
webRequest.Accept=“text/xml”
webRequest.Timeout=100000000
webRequest.Method=“POST”
#我们设置SOAPAction头-这告诉端点要运行什么服务
webRequest.Headers.Add('SOAPAction','http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP"') 
#我们将有效负载转换为字节数组,以允许我们将其写入请求流
encoding=System.Text.UTF8Encoding();
byte1=encoding.GetBytes(ReadBoard);
#将上面的字节写入请求
rqt=webRequest.GetRequestStream()
rqt.Write(字节1,0,字节1.Length)
#在这里获取响应
rsp=webRequest.GetResponse()
#创建一个流读取器,将原始响应转换为文本的UTF8
readStream=StreamReader(rsp.GetResponseStream(),System.Text.Encoding.UTF8)
#将流中的数据读入字符串
data=readStream.ReadToEnd()
readStream.Close()
rsp.Close()
#在这里处理数据
打印数据
除System.Net.WebException外,我们:
打印“Web异常:”,we.Message
如果we.Status==System.Net.WebExceptionStatus.ProtocolError:
打印int(we.Response.StatusCode)、we.Response.StatusDescription
打印(StreamReader(we.Response.GetResponseStream(),System.Text.Encoding.UTF8)).ReadToEnd()
除例外情况外,e:
打印e

当您发出指示SOAPAction的请求时,需要添加一个标题

比如:

webRequest.Headers.Add('SOAPAction', '"http://myservice.com/1/poll"') 
下面是调用Web服务的完整代码。在这种情况下

导入clr
从System.Net导入WebClient
从System.IO导入StreamReader
导入系统文本
从系统导入Uri
从System.Net导入HttpWebRequest
#这是SOAP负载
#只需要用基本的concats构建它
ReadBoard=“”
10011
"""
uriString=”http://wsf.cdyne.com/WeatherWS/Weather.asmx"
uri=uri(uri字符串)
尝试:
webRequest=HttpWebRequest.Create(uri)
webRequest.ContentType=“text/xml;charset=\”utf-8“
webRequest.Accept=“text/xml”
webRequest.Timeout=100000000
webRequest.Method=“POST”
#我们设置SOAPAction头-这告诉端点要运行什么服务
webRequest.Headers.Add('SOAPAction','http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP"') 
#我们将有效负载转换为字节数组,以允许我们将其写入请求流
encoding=System.Text.UTF8Encoding();
byte1=encoding.GetBytes(ReadBoard);
#将上面的字节写入请求
rqt=webRequest.GetRequestStream()
rqt.Write(字节1,0,字节1.Length)
#在这里获取响应
rsp=webRequest.GetResponse()
#创建一个流读取器,将原始响应转换为文本的UTF8
readStream=StreamReader(rsp.GetResponseStream(),System.Text.Encoding.UTF8)
#将流中的数据读入字符串
data=readStream.ReadToEnd()
readStream.Close()
rsp.Close()
#在这里处理数据
打印数据
import clr

from System.Net import WebClient
from System.IO import StreamReader
import System.Text
from System import Uri
from System.Net import HttpWebRequest

#This is the SOAP Payload 
#Just build it with basic concats
readerboard = """<?xml version="1.0" encoding="utf-8"?>
<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/">
  <soap:Body>
    <GetCityWeatherByZIP xmlns="http://ws.cdyne.com/WeatherWS/">
      <ZIP>10011</ZIP>
    </GetCityWeatherByZIP>
  </soap:Body>
</soap:Envelope>"""


uriString = "http://wsf.cdyne.com/WeatherWS/Weather.asmx"
uri = Uri(uriString)

try: 
    webRequest = HttpWebRequest.Create(uri)
    webRequest.ContentType = "text/xml;charset=\"utf-8\""
    webRequest.Accept = "text/xml"
    webRequest.Timeout = 1000000000
    webRequest.Method = "POST"
    #We set the SOAPAction Header - this tells the end point what service to run
    webRequest.Headers.Add('SOAPAction', '"http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP"') 

    #we turn the payload into a byte array to allow us to write it to the request stream
    encoding = System.Text.UTF8Encoding();
    byte1 = encoding.GetBytes(readerboard);
    #write the bytes above to the request
    rqt = webRequest.GetRequestStream()
    rqt.Write(byte1,0,byte1.Length)
    #get the response here
    rsp = webRequest.GetResponse()
    #create a stream reader to convert the raw response into UTF8 for text
    readStream = StreamReader(rsp.GetResponseStream(), System.Text.Encoding.UTF8)
    #read the data from the stream into a string
    data = readStream.ReadToEnd()
    readStream.Close()
    rsp.Close()
    #Do stuff with the data here
    print data
except System.Net.WebException, we:
    print "Web Exception:", we.Message
    if we.Status == System.Net.WebExceptionStatus.ProtocolError: 
        print int(we.Response.StatusCode), we.Response.StatusDescription
        print (StreamReader(we.Response.GetResponseStream(), System.Text.Encoding.UTF8)).ReadToEnd()
except Exception, e:
    print e