Service call with URL error?

I call the service dynamics in the URL.my class to call work.but, when I send a variable with an empty value, then in the service I get the value of the variable "\ n" for this. my class call service:

public class MyServiceDynamic
{
    public static string CallWebService(string ServiceURL,string ServiceOPname,List<SOAPDataClass> Parameters)
    {

        XmlDocument soapEnvelopeXml = CreateSoapEnvelope(ServiceOPname,Parameters);
        HttpWebRequest webRequest = CreateWebRequest(ServiceURL, ServiceURL + "?op=" + ServiceOPname);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

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

        // suspend this thread until call is complete. You might want to
        // do something usefull here like update your UI.
        asyncResult.AsyncWaitHandle.WaitOne();
        //asyncResult.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
        // 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);
        }
        BLLBase bb=new BLLBase();
        XmlDocument Doc = new XmlDocument();
        Doc.LoadXml(soapResult);
        string result = Doc.GetElementsByTagName(ServiceOPname + "Result")[0].InnerText;
        if (result.Substring(0, 1) == "[")
            return result;
        else if (bb.IsNumeric(result))
            return result;
        else
            throw new Exception(result);
    }

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

    private static XmlDocument CreateSoapEnvelope(string OPname, List<SOAPDataClass> Parameters)
    {
        XmlDocument soapEnvelop = new XmlDocument();
        string xml = string.Concat("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><", OPname, " xmlns=\"http://tempuri.org/\">");
        for (int i = 0; i < Parameters.Count; i++)
        {
            xml = string.Concat(xml, "<", Parameters[i].Properties, ">", Parameters[i].Value == "" ? null : Parameters[i].Value, "</", Parameters[i].Properties, ">");
        }
        xml = string.Concat(xml, "</", OPname, "></soap12:Body></soap12:Envelope>"); ;
        soapEnvelop.LoadXml(xml);
        return soapEnvelop;
    }

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

      

Crater class for soap class:

public class SOAPDataClass
{
    public string Properties;
    public string Value;
}

      

service code:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public string HelloWorld(string s)
    {
        return s;
    }

      

I am successfully calling the service with the code:

public string test()
    {
        List<SOAPDataClass> parameters = new List<SOAPDataClass>();
        parameters.Add(new SOAPDataClass() { Properties = "s", Value = "" });
        return MyServiceDynamic.CallWebService("http://myservice.com/service.asmx", "HelloWorld", parameters);
    }

      

when s is empty, the value of s in the service: enter image description here

how to fix it?

0


source to share


1 answer


fix the CreateSoapEnvelope code:



private static XmlDocument CreateSoapEnvelope(string OPname, List<SOAPDataClass> Parameters)
    {
        XmlDocument soapEnvelop = new XmlDocument();
        string xml = string.Concat("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><", OPname, " xmlns=\"http://tempuri.org/\">");
        for (int i = 0; i < Parameters.Count; i++)
        {
            if (!string.IsNullOrEmpty( Parameters[i].Value) )
            {
                if (Parameters[i].Value.Contains("<") || Parameters[i].Value.Contains(">"))
                    xml = string.Concat(xml, "<", Parameters[i].Properties, ">", @"<![CDATA[", @Parameters[i].Value, "]]>", "</", Parameters[i].Properties, ">");
                else
                    xml = string.Concat(xml, "<", Parameters[i].Properties, ">",  @Parameters[i].Value,  "</", Parameters[i].Properties, ">");
            }
            else
                xml = string.Concat(xml, "<", Parameters[i].Properties, "/>");

        }
        xml = string.Concat(xml, "</", OPname, "></soap12:Body></soap12:Envelope>"); ;
        soapEnvelop.LoadXml(xml);
        return soapEnvelop;
    }

      

0


source







All Articles