C#肥皂解析对类的响应

时间:2018-11-28 07:46:04

标签: c# soap login system.net.httpwebrequest

我这样做了:

public static void CallWebService()
    {
        var _url = "https://***.***.net.pl:447/***/***";
        var _action = "";

        XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
        HttpWebRequest webRequest = CreateWebRequest(_url, _action);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

        // begin async call to web request.
        IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
        asyncResult.AsyncWaitHandle.WaitOne();

        // get the response from the completed web request.
        string soapResult;
        using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
        {
            using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
            {
                soapResult = rd.ReadToEnd();
            }
            Console.Write(soapResult);
        }
    }

    private static HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static XmlDocument CreateSoapEnvelope()
    {
        XmlDocument soapEnvelopeDocument = new XmlDocument();
        soapEnvelopeDocument.LoadXml(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tas=""http://***.ws.com/""> <soapenv:Header/> <soapenv:Body> <tas:login> <firm>ENERIS KIELCE</firm> <login>***</login> <password>***</password> <device_id>***</device_id> </tas:login> </soapenv:Body> </soapenv:Envelope>");
        return soapEnvelopeDocument;
    }

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }

这是我的登录响应:

        "<?xml version=\"1.0\" ?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">
    <S:Body><ns2:loginResponse xmlns:ns2=\"http://tasks.ws.com/\">
<LoginResult><status>1</status><uid>fb2ea073-7c916ae9-a4f1-4a2b-ab50-8d8a09e57164</uid><mapServerList><SERVER><name>***MapCenterCache</name>

<URL>**** </URL><maxZoom>16</maxZoom></SERVER><SERVER><name>OSM Mapnik</name><URL>http://tile.openstreetmap.org/%zoom%/%x%/%y%.png</URL><waterMarkText>OpenStreetMap contributors</waterMarkText><waterMarkURL>http://www.openstreetmap.org/copyright</waterMarkURL><maxZoom>16</maxZoom></SERVER><SERVER><name>OpenCycleMap</name><URL>http://tile.opencyclemap.org/cycle/%zoom%/%x%/%y%.png</URL>
<maxZoom>16</maxZoom></SERVER><SERVER><name>OpenCycleMap Transport</name><URL>http://tile2.opencyclemap.org/transport/%zoom%/%x%/%y%.png</URL><maxZoom>16</maxZoom></SERVER><SERVER><name>MapQuest</name><URL>http://otile1.mqcdn.com/tiles/1.0.0/osm/%zoom%/%x%/%y%.jpg</URL><maxZoom>16</maxZoom></SERVER></mapServerList></LoginResult></ns2:loginResponse></S:Body></S:Envelope>"

而且我不知道如何读取此soap动作,如何解析对c#类的响应。我想发送登录请求并解析对C#的响应,因为我必须在我的应用程序中使用此参数

0 个答案:

没有答案