Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

c# - Web Service without adding a reference?

I have 3 web services added to service references in a class library.(This is a sample project for an API use) I need to move these into my project but i cannot add the service references because of the security issues(By security issues i mean the service only responds to one ip address and that is the ip address of our customer's server.) Is there a way to generate a class like using "Ildasm.exe" for that particaluar web service?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this class. I didn't remember where i found the basic code, i added some methods and convert to class before.

public class WebService
{
    public string Url { get; set; }
    public string MethodName { get; set; }
    public Dictionary<string, string> Params = new Dictionary<string, string>();
    public XDocument ResultXML;
    public string ResultString;

    public WebService()
    {

    }

    public WebService(string url, string methodName)
    {
        Url = url;
        MethodName = methodName;
    }

    /// <summary>
    /// Invokes service
    /// </summary>
    public void Invoke()
    {
        Invoke(true);
    }

    /// <summary>
    /// Invokes service
    /// </summary>
    /// <param name="encode">Added parameters will encode? (default: true)</param>
    public void Invoke(bool encode)
    {
        string soapStr =
            @"<?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>
                <{0} xmlns=""http://tempuri.org/"">
                  {1}
                </{0}>
              </soap:Body>
            </soap:Envelope>";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
        req.Headers.Add("SOAPAction", ""http://tempuri.org/" + MethodName + """);
        req.ContentType = "text/xml;charset="utf-8"";
        req.Accept = "text/xml";
        req.Method = "POST";

        using (Stream stm = req.GetRequestStream())
        {
            string postValues = "";
            foreach (var param in Params)
            {
                if (encode)
                    postValues += string.Format("<{0}>{1}</{0}>", HttpUtility.UrlEncode(param.Key), HttpUtility.UrlEncode(param.Value));
                else
                    postValues += string.Format("<{0}>{1}</{0}>", param.Key, param.Value);
            }

            soapStr = string.Format(soapStr, MethodName, postValues);
            using (StreamWriter stmw = new StreamWriter(stm))
            {
                stmw.Write(soapStr);
            }
        }

        using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))
        {
            string result = responseReader.ReadToEnd();
            ResultXML = XDocument.Parse(result);
            ResultString = result;
        }
    }
}

And you can use like this

WebService ws = new WebService("service_url", "method_name");
ws.Params.Add("param1", "value_1");
ws.Params.Add("param2", "value_2");
ws.Invoke();
// you can get result ws.ResultXML or ws.ResultString

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...