Web 服务返回时如何避免 HTML 脚本

发布于 2024-11-16 19:06:28 字数 1401 浏览 2 评论 0原文

[ScriptMethod(UseHttpGet = true)]
[WebMethod]

public string sampleTest()
{
    string name = "";
    name = System.Web.HttpContext.Current.Request.QueryString["name"];

    StringBuilder sb = new StringBuilder();
    sb.Append("<message>");
    sb.Append("<categoryname =" + name + "/>");
    sb.Append("</message>");
    return sb.ToString();
}

调用方法:

StringBuilder sb = new StringBuilder();

    byte[] buf = new byte[8192];


    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/mylytica/apibridge.asmx/sampleTest?name=Shankar");

    HttpWebResponse response = (HttpWebResponse)
        request.GetResponse();

    Stream resStream = response.GetResponseStream();

    string tempString = null;
    int count = 0;

    do
    {
        count = resStream.Read(buf, 0, buf.Length);
        if (count != 0)
        {
            tempString = Encoding.ASCII.GetString(buf, 0, count);
            sb.Append(tempString);
        }
    }

    while (count > 0);

    Literal1.Text = sb.ToString();

文字值包含:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">&lt;message loginstatus="OK" userid="1" /&gt;</string>

实际格式:

<message loginstatus="OK" userid="1" />

我必须做什么。

[ScriptMethod(UseHttpGet = true)]
[WebMethod]

public string sampleTest()
{
    string name = "";
    name = System.Web.HttpContext.Current.Request.QueryString["name"];

    StringBuilder sb = new StringBuilder();
    sb.Append("<message>");
    sb.Append("<categoryname =" + name + "/>");
    sb.Append("</message>");
    return sb.ToString();
}

Invoking method :

StringBuilder sb = new StringBuilder();

    byte[] buf = new byte[8192];


    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/mylytica/apibridge.asmx/sampleTest?name=Shankar");

    HttpWebResponse response = (HttpWebResponse)
        request.GetResponse();

    Stream resStream = response.GetResponseStream();

    string tempString = null;
    int count = 0;

    do
    {
        count = resStream.Read(buf, 0, buf.Length);
        if (count != 0)
        {
            tempString = Encoding.ASCII.GetString(buf, 0, count);
            sb.Append(tempString);
        }
    }

    while (count > 0);

    Literal1.Text = sb.ToString();

Literal value contains:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/"><message loginstatus="OK" userid="1" /></string>

Actual Format:

<message loginstatus="OK" userid="1" />

What i have to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2024-11-23 19:06:28
[ScriptMethod(UseHttpGet = true)]
[WebMethod]


public XmlDocument login(string username, string password)
{
       XmlDocument oXmlDoc = new XmlDocument();
       XmlNode oXmlmessage = oXmlDoc.CreateNode(XmlNodeType.Element, "message", "");
       XmlAttribute oxmllogin = oXmlDoc.CreateAttribute("loginstatus");
       oxmllogin.InnerText = "OK";

       XmlAttribute oXmluserid = oXmlDoc.CreateAttribute("userid");
       oXmluserid.InnerText = iRegID.ToString();

       oXmlmessage.Attributes.Append(oxmllogin);
       oXmlmessage.Attributes.Append(oXmluserid);

       oXmlDoc.AppendChild(oXmlmessage);

       return oXmlDoc;
}
[ScriptMethod(UseHttpGet = true)]
[WebMethod]


public XmlDocument login(string username, string password)
{
       XmlDocument oXmlDoc = new XmlDocument();
       XmlNode oXmlmessage = oXmlDoc.CreateNode(XmlNodeType.Element, "message", "");
       XmlAttribute oxmllogin = oXmlDoc.CreateAttribute("loginstatus");
       oxmllogin.InnerText = "OK";

       XmlAttribute oXmluserid = oXmlDoc.CreateAttribute("userid");
       oXmluserid.InnerText = iRegID.ToString();

       oXmlmessage.Attributes.Append(oxmllogin);
       oXmlmessage.Attributes.Append(oXmluserid);

       oXmlDoc.AppendChild(oXmlmessage);

       return oXmlDoc;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文