调用 Web 服务失败,HTTP 状态为 400 错误请求

发布于 2024-09-13 09:44:15 字数 7482 浏览 1 评论 0原文

我的 Web 应用程序调用 Web 服务失败:

“/zVersion2a”应用程序中出现服务器错误。 请求失败,HTTP 状态为 400:错误请求。

以下是 Web 服务本身的链接(有效):

http://www.zipeee.com/Zipeeewebservice/

以下是[生产] Web 应用程序的链接,该应用程序在首页加载时调用 WS:

http://www.zipeee.com/Zipeee/

这是指向 [newVersion] Web 应用程序的链接,该应用程序无法调用同一 WS:

http://www.zipeee.com/zVersion2/

这是调用页面中的源代码:

Imports System.Data
Partial Class frmZipeee
Inherits System.Web.UI.Page
Public wsZipeee As New ProxyZipeeeService.WSZipeee.Zipeee
Dim dsStandardMsg As DataSet
Dim dtStandardMsg As DataTable
Dim dsRandomMsg As DataSet
Dim dtRandomMsg As DataTable
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
        LoadMessage()
    End If
End Sub
Private Sub LoadMessage()
    Dim iCnt As Integer
    Dim iValue As Integer

    dsStandardMsg = Nothing
    dsStandardMsg = wsZipeee.GetMessageByType(BizConstants.MsgType.Standard)
    iCnt = dsStandardMsg.Tables(0).Rows.Count
        If iCnt = 0 Then
            Me.btnEnter.Text = "Click Here to Enter."
        Else
            Me.btnEnter.Text = dsStandardMsg.Tables(0).Rows(0).Item("MessageContent")
        End If
    dsStandardMsg.Dispose()
End Sub
End Class

这是代理:

<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/ZipeeeWebService/Zipeee/Get Message By Type", RequestElementName:="Get Message By Type", RequestNamespace:="http://tempuri.org/ZipeeeWebService/Zipeee", ResponseElementName:="Get Message By TypeResponse", ResponseNamespace:="http://tempuri.org/ZipeeeWebService/Zipeee", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _
    Public Function GetMessageByType(ByVal iMsgType As Integer) As <System.Xml.Serialization.XmlElementAttribute("Get Message By TypeResult")> System.Data.DataSet
        Dim results() As Object = Me.Invoke("GetMessageByType", New Object() {iMsgType})
        Return CType(results(0),System.Data.DataSet)
    End Function

最后,这是webservice中的代码:

   <WebMethod(MessageName:="Get Message By Type")> _
           Public Function GetMessageByType(ByVal iMsgType As Integer) As DataSet
    Try
        'create a connection...
        con = New SqlConnection(sConnZipeee)
        con.Open()
        command = New SqlCommand("spGetMessageByType", con)
        command.CommandType = CommandType.StoredProcedure
        AppendParm(command, "@MessageType", SqlDbType.Int, iMsgType)
        Dim adp As SqlDataAdapter = New SqlDataAdapter(command)
        Dim ds As New DataSet
        adp.Fill(ds, "TableMsg")
        adp.Dispose()
        command.Dispose()
        con.Close()
        Return ds
    Catch ex As SqlException
     ...etc.                'As I read the trace output (below) we never got here nor to SQL

在运行 IIS 6.0 Windows 2003 的生产 Web 服务器上,默认网站内有 3 个虚拟目录(Zipeee、zVersion2、ZipeeeWebService)。

每个虚拟目录都指向 Web 服务器 C 盘上的一个单独的物理目录。生产版 Zipeee 和新版本 zVersion2 都使用名为 ProxyZipeeeService.dll 的代理类。我非常小心地发布主项目 Zipeee 及其所需的网络服务。例如,在使用 Visual Studio2008 的开发服务器上,我构建并测试了所有 5 个项目(webservice1、webservice2、primarywebapp (iezipeee)、proxyclassforWS1、proxyclassforWS2)。所有这些 DLL 的最新版本都标有选择为 1.6 的程序集版本。它们都在我的开发机器上工作,我将 WS1、WS2 和 zipeee 发布到临时服务器 (IIS 6.0),它们也都在那里工作。然后我发布到托管上述 URL 的公共服务器。新版本的 Web 应用程序进入 C 驱动器上的单独文件夹,并由 vDir zVersion2 指向。它应该可以工作,但由于此 WS 错误,甚至无法渲染第一页。

我跟踪了 zVersion2 的失败(它在页面加载事件中调用 webmethod GetMessageByType 并立即失败)。这是(不是很长,但是这里的SO格式有点混乱):

System.Web.Services.Asmx信息:0:调用XmlSerializer [创建XmlSerializer] 方法:System.Xml.Serialization.XmlSerializer#1669721556::FromMappings(System.Xml.Serialization.XmlMapping[]#40041277=[356],ProxyZipeeeService.WSZipeee.Zipeee#50200644=ProxyZipeeeService.WSZipeee.Zipeee) 调用者:System.Web.Services.Protocols.SoapClientType#24827179::.ctor(ProxyZipeeeService.WSZipeee.Zipeee#50200644=ProxyZipeeService.WSZipeee.Zipeee) 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:45.6541439Z 时间戳=47298006836 System.Web.Services.Asmx信息:0:从XmlSerializer返回[创建XmlSerializer] 调用者:System.Web.Services.Protocols.SoapClientType#24827179::.ctor(ProxyZipeeeService.WSZipeee.Zipeee#50200644=ProxyZipeeeService.WSZipeee.Zipeee) 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:45.7791255Z 时间戳=47298376293 System.Web.Services.Asmx信息:0:调用XmlSerializer [创建XmlSerializer] 方法:System.Xml.Serialization.XmlSerializer#1669721556::FromMappings(System.Xml.Serialization.XmlMapping[]#64844482=[160],ProxyEmergencyService.WSEmergency.Emergency#88831384=ProxyEmergencyService.WSEmergency.Emergency) 调用者:System.Web.Services.Protocols.SoapClientType#46729429::.ctor(ProxyEmergencyService.WSEmergency.Emergency#88831384=ProxyEmergencyService.WSEmergency.Emergency) 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:45.8572390Z 时间戳=47298680452 System.Web.Services.Asmx信息:0:从XmlSerializer返回[创建XmlSerializer] 调用者:System.Web.Services.Protocols.SoapClientType#46729429::.ctor(ProxyEmergencyService.WSEmergency.Emergency#88831384=ProxyEmergencyService.WSEmergency.Emergency) 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:45.9353525Z 时间戳=47298924420 System.Web.Services.Asmx信息:0:调用XmlSerializer [写入请求] 方法: Microsoft.Xml.Serialization.GenerateAssembly.ArrayOfObjectSerializer192#13256970::Serialize(System.Xml.XmlTextWriter#52203868=.., System.Object[]#72766=[1], (null), (null)) 调用者:ProxyZipeeService.WSZipeee.Zipeee#654897::Serialize() 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:46.0915795Z 时间戳=47299598906 System.Web.Services.Asmx信息:0:从XmlSerializer返回[写入请求] 调用者:ProxyZipeeService.WSZipeee.Zipeee#654897::Serialize() 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:46.0915795Z 时间戳=47299606361 System.Web.Services.Asmx信息:0:调用WebRequest.GetResponse 方法:System.Net.HttpWebRequest#5894079::GetResponse() 调用者:ProxyZipeeService.WSZipeee.Zipeee#654897::GetWebResponse() 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:46.0915795Z 时间戳=47299607492 System.Web.Services.Asmx 错误:0:在 ProxyZipeeService.WSZipeee.Zipeee#654897::GetWebResponse 中捕获异常。 System.Net.WebException:远程服务器返回错误:(400) 错误请求。 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:46.0915795Z 时间戳=47299629487 System.Web.Services.Asmx 错误:0:异常详细信息: System.Net.WebException:远程服务器返回错误:(400) 错误请求。 在 System.Net.HttpWebRequest.GetResponse() 在 System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest 请求) 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:46.0915795Z 时间戳=47299654807 System.Web.Services.Asmx信息:0:调用XmlSerializer [写入请求] 方法: Microsoft.Xml.Serialization.GenerateAssembly.ArrayOfObjectSerializer192#13256970::Serialize(System.Xml.XmlTextWriter#53046711=.., System.Object[]#7658356=[1], (null), (null)) 调用者:ProxyZipeeService.WSZipeee.Zipeee#1816341::Serialize() 进程 ID=1456 逻辑操作栈= 线程 ID=1 日期时间=2010-08-06T01:58:56.9024879Z 时间戳=47338202360

I'm getting a failure in my web application calling a webservice:

Server Error in '/zVersion2a' Application.
The request failed with HTTP status 400: Bad Request.

Here is a link to the webservice itself (this works):

http://www.zipeee.com/Zipeeewebservice/

Here is a link to the [production] web app that calls the WS on the first page load:

http://www.zipeee.com/Zipeee/

Here is a link to the [newVersion] web app that fails calling this same WS:

http://www.zipeee.com/zVersion2/

Here is the source in the calling page:

Imports System.Data
Partial Class frmZipeee
Inherits System.Web.UI.Page
Public wsZipeee As New ProxyZipeeeService.WSZipeee.Zipeee
Dim dsStandardMsg As DataSet
Dim dtStandardMsg As DataTable
Dim dsRandomMsg As DataSet
Dim dtRandomMsg As DataTable
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
        LoadMessage()
    End If
End Sub
Private Sub LoadMessage()
    Dim iCnt As Integer
    Dim iValue As Integer

    dsStandardMsg = Nothing
    dsStandardMsg = wsZipeee.GetMessageByType(BizConstants.MsgType.Standard)
    iCnt = dsStandardMsg.Tables(0).Rows.Count
        If iCnt = 0 Then
            Me.btnEnter.Text = "Click Here to Enter."
        Else
            Me.btnEnter.Text = dsStandardMsg.Tables(0).Rows(0).Item("MessageContent")
        End If
    dsStandardMsg.Dispose()
End Sub
End Class

Here is the code from Reference.vb in the proxy:

<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/ZipeeeWebService/Zipeee/Get Message By Type", RequestElementName:="Get Message By Type", RequestNamespace:="http://tempuri.org/ZipeeeWebService/Zipeee", ResponseElementName:="Get Message By TypeResponse", ResponseNamespace:="http://tempuri.org/ZipeeeWebService/Zipeee", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _
    Public Function GetMessageByType(ByVal iMsgType As Integer) As <System.Xml.Serialization.XmlElementAttribute("Get Message By TypeResult")> System.Data.DataSet
        Dim results() As Object = Me.Invoke("GetMessageByType", New Object() {iMsgType})
        Return CType(results(0),System.Data.DataSet)
    End Function

Finally, here is the code in the webservice:

   <WebMethod(MessageName:="Get Message By Type")> _
           Public Function GetMessageByType(ByVal iMsgType As Integer) As DataSet
    Try
        'create a connection...
        con = New SqlConnection(sConnZipeee)
        con.Open()
        command = New SqlCommand("spGetMessageByType", con)
        command.CommandType = CommandType.StoredProcedure
        AppendParm(command, "@MessageType", SqlDbType.Int, iMsgType)
        Dim adp As SqlDataAdapter = New SqlDataAdapter(command)
        Dim ds As New DataSet
        adp.Fill(ds, "TableMsg")
        adp.Dispose()
        command.Dispose()
        con.Close()
        Return ds
    Catch ex As SqlException
     ...etc.                'As I read the trace output (below) we never got here nor to SQL

On this production webserver running IIS 6.0 Windows 2003, there are 3 virtual directories within the default web site (Zipeee,zVersion2,ZipeeeWebService).

Each virtual directory points to a separate physical dir on the c drive of the webserver. Both the production Zipeee and the newVersion zVersion2 make use of proxy class called ProxyZipeeeService.dll. I've taken great care in the publishing of the main project Zipeee and the webservices it needs. For example, on a developement server using Visual Studio2008, I've built and tested all 5 projects (webservice1, webservice2, primarywebapp (i.e.zipeee), proxyclassforWS1, proxyclassforWS2). The newest versions of all of these DLLs was tagged with an assembly version chosen to be 1.6. They all work on my dev machine and I published WS1,WS2 and zipeee to a staging server (IIS 6.0) and they all work there too. Then I publish to the public server hosting the URLs above. The new version of the web app goes into separate folders on the c drive and pointed to by vDir zVersion2. It should work but fails to render even the first page because of this WS error.

I traced the failure of zVersion2 (it calls webmethod GetMessageByType in page load event and fails right away). Here it is (not very long but formatting in SO here is a bit messy):

System.Web.Services.Asmx Information: 0 : Calling XmlSerializer [Create XmlSerializer]
Method: System.Xml.Serialization.XmlSerializer#1669721556::FromMappings(System.Xml.Serialization.XmlMapping[]#40041277=[356], ProxyZipeeeService.WSZipeee.Zipeee#50200644=ProxyZipeeeService.WSZipeee.Zipeee)
Caller: System.Web.Services.Protocols.SoapClientType#24827179::.ctor(ProxyZipeeeService.WSZipeee.Zipeee#50200644=ProxyZipeeeService.WSZipeee.Zipeee)
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:45.6541439Z
Timestamp=47298006836
System.Web.Services.Asmx Information: 0 : Return from XmlSerializer [Create XmlSerializer]
Caller: System.Web.Services.Protocols.SoapClientType#24827179::.ctor(ProxyZipeeeService.WSZipeee.Zipeee#50200644=ProxyZipeeeService.WSZipeee.Zipeee)
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:45.7791255Z
Timestamp=47298376293
System.Web.Services.Asmx Information: 0 : Calling XmlSerializer [Create XmlSerializer]
Method: System.Xml.Serialization.XmlSerializer#1669721556::FromMappings(System.Xml.Serialization.XmlMapping[]#64844482=[160], ProxyEmergencyService.WSEmergency.Emergency#88831384=ProxyEmergencyService.WSEmergency.Emergency)
Caller: System.Web.Services.Protocols.SoapClientType#46729429::.ctor(ProxyEmergencyService.WSEmergency.Emergency#88831384=ProxyEmergencyService.WSEmergency.Emergency)
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:45.8572390Z
Timestamp=47298680452
System.Web.Services.Asmx Information: 0 : Return from XmlSerializer [Create XmlSerializer]
Caller: System.Web.Services.Protocols.SoapClientType#46729429::.ctor(ProxyEmergencyService.WSEmergency.Emergency#88831384=ProxyEmergencyService.WSEmergency.Emergency)
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:45.9353525Z
Timestamp=47298924420
System.Web.Services.Asmx Information: 0 : Calling XmlSerializer [Write Request]
Method: Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer192#13256970::Serialize(System.Xml.XmlTextWriter#52203868=.., System.Object[]#72766=[1], (null), (null))
Caller: ProxyZipeeeService.WSZipeee.Zipeee#654897::Serialize()
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:46.0915795Z
Timestamp=47299598906
System.Web.Services.Asmx Information: 0 : Return from XmlSerializer [Write Request]
Caller: ProxyZipeeeService.WSZipeee.Zipeee#654897::Serialize()
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:46.0915795Z
Timestamp=47299606361
System.Web.Services.Asmx Information: 0 : Calling WebRequest.GetResponse
Method: System.Net.HttpWebRequest#5894079::GetResponse()
Caller: ProxyZipeeeService.WSZipeee.Zipeee#654897::GetWebResponse()
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:46.0915795Z
Timestamp=47299607492
System.Web.Services.Asmx Error: 0 : Exception caught in ProxyZipeeeService.WSZipeee.Zipeee#654897::GetWebResponse.
System.Net.WebException: The remote server returned an error: (400) Bad Request.
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:46.0915795Z
Timestamp=47299629487
System.Web.Services.Asmx Error: 0 : Exception Details:
System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:46.0915795Z
Timestamp=47299654807
System.Web.Services.Asmx Information: 0 : Calling XmlSerializer [Write Request]
Method: Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer192#13256970::Serialize(System.Xml.XmlTextWriter#53046711=.., System.Object[]#7658356=[1], (null), (null))
Caller: ProxyZipeeeService.WSZipeee.Zipeee#1816341::Serialize()
ProcessId=1456
LogicalOperationStack=
ThreadId=1
DateTime=2010-08-06T01:58:56.9024879Z
Timestamp=47338202360

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

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

发布评论

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

评论(2

爱的那么颓废 2024-09-20 09:44:15

尝试执行以下操作:
1. 添加一个名为 wsZipee 的新服务引用,指向 Web 服务
2. 从调用位置(另一个 Web 应用程序、Winform 等):

Dim ws as New wsZipeee.ZipeeeSoapClient()
Dim dsStandardMsg As DataSet 
dsStandardMsg = ws.GetMessageByType(BizConstants.MsgType.Standard)

通过创建新的服务引用,这将处理代理并允许您干净地调用它。我假设您要调用的服务引用、Web 服务和方法的名称

Try to do the following:
1. Add a new Service Reference named wsZipeee pointing to the Web Service
2. From the Calling location (another Web App, Winform, et al):

Dim ws as New wsZipeee.ZipeeeSoapClient()
Dim dsStandardMsg As DataSet 
dsStandardMsg = ws.GetMessageByType(BizConstants.MsgType.Standard)

By creating a new Service Reference, this will handle the proxy and allow you to cleanly call it. I am assuming the name of the service reference, web service, and method you want to call

笑脸一如从前 2024-09-20 09:44:15

如上所述,主站点使用代理 DLL 与 Web 服务进行通信。在 Visual Studio 中,每个代理项目都有一个 Web 引用(.wsdl 文件等)和一个生成的 app.config 文件。在这 2 个代理项目之一中,app.config 文件保留了原始设置(这当然适用于我的桌面开发计算机和临时 Web 服务器):

<applicationSettings>
    <ProxyZipeeeService.My.MySettings>
        <setting name="ProxyZipeeeService_WSZipeee_Zipeee" serializeAs="String">
            <value>http://localhost/ZipeeeWebService/Zipeee.asmx</value>
        </setting>
    </ProxyZipeeeService.My.MySettings>
</applicationSettings>

请注意上面的 localhost 值。

事实证明,当部署到生产服务器时,同一 IIS 计算机上的默认网站和另一个网站都有一个主机标头值。后来我发现了更多关于此设置的后果,并意识到当主站点尝试通过 localhost 访问 Web 服务时,它失败了,因为在 2 个主机标头值中找不到 localhost。因此,返回 Vstudio 并按如下方式更改 Web 引用解决了问题:

<applicationSettings>
    <ProxyZipeeeService.My.MySettings>
        <setting name="ProxyZipeeeService_WSZipeee_Zipeee" serializeAs="String">
            <value>http://www.zipeee.com/ZipeeeWebService/Zipeee.asmx</value>
        </setting>
    </ProxyZipeeeService.My.MySettings>
</applicationSettings>

As noted above, the main site used Proxy DLLs to communicate to the web service(s). In Visual Studio each Proxy project had a Web Reference (.wsdl file, etc.) and a generated app.config file. In one of these 2 proxy projects, the app.config file retained the original settings (which of course worked on my desktop dev machine and my staging web server):

<applicationSettings>
    <ProxyZipeeeService.My.MySettings>
        <setting name="ProxyZipeeeService_WSZipeee_Zipeee" serializeAs="String">
            <value>http://localhost/ZipeeeWebService/Zipeee.asmx</value>
        </setting>
    </ProxyZipeeeService.My.MySettings>
</applicationSettings>

Note the localhost value above.

It turns out that when deployed to the production server, there is a host header value for the default web site and another web site on the same IIS machine. Later I discovered more about the fallout from this setup and came to realize that when the main site tried to access the web service via localhost it failed because localhost is not found amoung the 2 host header values. Therefore going back to Vstudio and changing the web reference as follows fixed the problem:

<applicationSettings>
    <ProxyZipeeeService.My.MySettings>
        <setting name="ProxyZipeeeService_WSZipeee_Zipeee" serializeAs="String">
            <value>http://www.zipeee.com/ZipeeeWebService/Zipeee.asmx</value>
        </setting>
    </ProxyZipeeeService.My.MySettings>
</applicationSettings>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文