0

Currently I want to know that is it possible in c# to read string and convert it into Soapmessage and send it to server for processing and again convert response into the string ?

Ashok Rathod
  • 800
  • 7
  • 21
  • This may be what you're looking for: http://stackoverflow.com/questions/10294544/extract-soap-body-from-a-soap-message – dotNET Jul 31 '14 at 05:22
  • This one has more in-depth treatment of the topic: http://www.codeproject.com/Articles/445007/Intercept-a-raw-SOAP-message – dotNET Jul 31 '14 at 05:23

1 Answers1

3

Your code should look something like :

var document = Query(user, pass, date, regc);
var webRequest = Request(Url, Action, document);
var soapResult = Response(webRequest);
happiness = soapResult.Deserialize(out result);

Query:

internal static string Query(string user, string pass, string date, string regc)
{
    var doc = string.Format(@"<?xml version='1.0' encoding='UTF-8'?>
        <s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>
            <s:Body xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
            <PersonnelInfo xmlns='http://propartner.ee/'>
                <PersonnelInfoRequest xmlns=''>
                <username>{0}</username>
                <password>{1}</password>
                <reg_code>{2}</reg_code>
                <date>{3}</date>
                <country>est</country>
                <language>est</language>
                </PersonnelInfoRequest>
            </PersonnelInfo>
            </s:Body>
        </s:Envelope>", user, pass, regc, date);
    return doc;
}

Request:

    internal static HttpWebRequest Request(string url, string action, string doc)
    {
        var soapEnvelop = new XmlDocument();
        soapEnvelop.LoadXml(doc);
        var webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        webRequest.Host = "wsrvc1.propartner.ee";
        using (var stream = webRequest.GetRequestStream())
        {
            soapEnvelop.Save(stream);
        }
        return webRequest;
    }

Response:

    internal static string Response(HttpWebRequest webRequest)
    {
        var asyncResult = webRequest.BeginGetResponse(null, null);
        asyncResult.AsyncWaitHandle.WaitOne();
        using (var webResponse = webRequest.EndGetResponse(asyncResult))
        {
            var responsesteam = webResponse.GetResponseStream();
            if (responsesteam == null) return default(string);
            using (var rd = new StreamReader(responsesteam))
            {
                var soapResult = rd.ReadToEnd();
                return soapResult;
            }
        }
    }
Margus
  • 18,332
  • 12
  • 51
  • 101