是否可以将简单字符串转换为 SOAP 消息并发送? [英] Is it possible that I can convert simple string to SOAP Message and send it?

查看:44
本文介绍了是否可以将简单字符串转换为 SOAP 消息并发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我想知道在 c# 中是否可以读取字符串并将其转换为 Soapmessage 并将其发送到服务器进行处理,然后再次将响应转换为字符串?

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 ?

推荐答案

你的代码应该类似于:

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

查询:

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;
}

请求:

    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;
    }

回复:

    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;
            }
        }
    }

这篇关于是否可以将简单字符串转换为 SOAP 消息并发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆