URL错误的呼叫服务?

时间:2016-05-03 08:06:31

标签: c# web-services

我在URL.my类中调用service dynamic来调用是work.but当send变量为空值然后在服务中我得到这个变量值" \ n" 。 我的班级电话服务是:

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

class soap value crater是:

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

服务代码是:

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

我使用代码成功调用服务:

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

当Variable s为空时,服务中Variable s的值为: enter image description here

如何解决?

1 个答案:

答案 0 :(得分:0)

修复CreateSoapEnvelope代码:

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