创建一个接受XML作为字符串的Web服务

时间:2015-04-09 07:25:57

标签: asp.net xml vb.net string web-services

我一直在尝试创建一个Web服务,需要允许客户端通过3个类型字符串的3个参数发送用户名,密码和XML_In。然后,它将变量发送到Stored过程,该过程处理数据并返回XML字符串,然后将其返回给客户端。

存储过程100%正常工作但我收到的错误是XML作为字符串发送。从阅读起来,大多数人都提出了将<httpRuntime requestValidationMode="2.0"/><pages validateRequest="false">添加到我的web.config的建议,但这可以适用于我完全不需要的整个网站。

另一个建议是将它放在@Page部分,但这不适用于Web服务,因为它没有用户定义的布局。请参阅下面的代码并提供帮助。我仍然是.Net的新手,因此我在SQL中做了90%的原因。

返回的错误是:

System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (XML_In=&quot;&lt;senddata&gt;&lt;settings ...&quot;).
   at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
   at System.Web.HttpRequest.ValidateHttpValueCollection(HttpValueCollection collection, RequestValidationSource requestCollection)
   at System.Web.HttpRequest.get_Form()
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

这是代码:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Xml
Imports JumpStart.Framework.Database

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://mydomainname.co.za/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class LeadProcessor
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function Lead_Processing(ByVal UserName As String, ByVal PassWord As String, ByVal XML_In As String) As XmlDocument
        Dim poDSL As New DSL
        Dim Result As String
        Dim XML_Out = New XmlDocument()

        Result = poDSL.ExecuteProcedure_ExecuteScalar("DECLARE @Result XML " & _
                                             "EXEC [dbo].[APP_InsertLeadFromXML] " & _
                                            "@Username = N'" & UserName & "', " & _
                                            "@Password = N'" & PassWord & "', " & _
                                            "@ParmListXML = '" & XML_In.ToString & "', " & _
                                            "@XMLResult = @Result OUTPUT " & _
                                            "SELECT @Result")

        XML_Out.LoadXml(Result)

        Return XML_Out
    End Function

1 个答案:

答案 0 :(得分:0)

我同意CodeCaster评论(新项目 - WCF或WebApi)。

但是如果你现在不能改变它,可以考虑使用XMLDocument作为参数,或者使用XElemento,或者直接对文本进行Base64转换,以避免错误。 < / p>

您的服务代码'客户:

your_proxy.Lead_Processing(sUserName, sPassWord, Convert.ToBase64String(Encoding.UTF8.GetBytes(sXML))

然后,在您的服务中,执行此操作

 <WebMethod()> _
    Public Function Lead_Processing(ByVal UserName As String, ByVal PassWord As String, ByVal XML_In As String) As XmlDocument

    Dim oData As Byte() = Convert.FromBase64String(XML_In)
    Dim sDecodedXML As String = System.Text.Encoding.UTF8.GetString(oData)

    Dim poDSL As New DSL
    Dim Result As String
    Dim XML_Out = New XmlDocument()

    Result = poDSL.ExecuteProcedure_ExecuteScalar("DECLARE @Result XML " & _
                                         "EXEC [dbo].[APP_InsertLeadFromXML] " & _
                                        "@Username = N'" & UserName & "', " & _
                                        "@Password = N'" & PassWord & "', " & _
                                        "@ParmListXML = '" & sDecodedXML  & "', " & _
                                        "@XMLResult = @Result OUTPUT " & _
                                        "SELECT @Result")

    XML_Out.LoadXml(Result)

    Return XML_Out
End Function

希望有所帮助

修改 使用base64的服务。 我们告诉Asmx它几乎是一种传统技术。新项目可能会使用WCF技术(我不能在几行中教你WCF)。 您可以将该代码放入asmx服务中。您可以按我编辑答案的方式使用该代码。