使用 URL 错误调用服务? [英] call service with URL mistake?

查看:21
本文介绍了使用 URL 错误调用服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 URL 中调用动态服务.我的类调用是有效的.但是当发送带有空值的变量然后在服务中我得到这个变量值 "\n " .我的课堂通话服务是:

I call service dynamic in URL.my class to call is work.but when send Variable with empty value then in service I get for this Variable value "\n " . my class call service is:

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

类肥皂值火山口是:

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

服务代码为:

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

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

I call service successfully with 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);
    }

当Variable s为空时,服务中的Variable s的值为:

when Variable s is empty ,value of Variable s in service is:

如何解决?

推荐答案

修复 CreateSoapEnvelope 代码:

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

这篇关于使用 URL 错误调用服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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