【问题标题】:posting data to asmx page using vb.net使用 vb.net 将数据发布到 asmx 页面
【发布时间】:2012-07-22 17:33:20
【问题描述】:

您好, 我正在尝试使用 vb.net 将数据发布到 asmx webservice,但收到以下错误“Error WebException The remote server returned an error: (500) Internal Server Error.”

我使用的vb.net编码是:

Private Function ConnectApi() As String
    Dim hwReq As HttpWebRequest
    Dim hwRes As HttpWebResponse



    Dim gsLogin As String = "xxxxxxx"
    Dim gsPassword As String = "xxxxxxxxx"
    Dim gsCode As String = "xxxxxxxxx"
    Dim mTransactionId As String = "521"
    Dim mBaseCurrency As String = "USD"
    Dim mPaymentValue As Decimal = 2.1
    Dim mDateTime As Date = Now.Date

    Dim mCustomValue As Integer = 15

    'Dim strPostData As String = String.Format("sub={0}&login={1}&password={2}&limit={3}", Server.UrlEncode(ApiSub), Server.UrlEncode(ApiUsername), Server.UrlEncode(ApiPassword), Server.UrlEncode(ApiLimit))
    Dim strPostData As String = String.Format("gsLogin={0}&gsPassword={1}&gsCode={2}&mTransactionId={3}&mBaseCurrency={4}&mPaymentValue={5}&mDateTime={6}&mCustomValue={7}", Server.UrlEncode(gsLogin), Server.UrlEncode(gsPassword), Server.UrlEncode(gsCode), Server.UrlEncode(mTransactionId), Server.UrlEncode(mBaseCurrency), Server.UrlEncode(mPaymentValue), Server.UrlEncode(mDateTime), Server.UrlEncode(mBaseCurrency), Server.UrlEncode(mCustomValue))



    Dim strResult As String = ""
    Try
        'https:/gscash.com/gateway/staging/gsprocess.asmx?wsdl

        hwReq = DirectCast(WebRequest.Create("https://gscash.com/gateway/staging/gsprocess.asmx"), HttpWebRequest)





        hwReq.Method = "POST"
        hwReq.ContentType = "application/x-www-form-urlencoded"
        hwReq.ContentLength = strPostData.Length

        Dim arrByteData As Byte() = ASCIIEncoding.ASCII.GetBytes(strPostData)
        hwReq.GetRequestStream().Write(arrByteData, 0, arrByteData.Length)

        hwRes = DirectCast(hwReq.GetResponse(), HttpWebResponse)
        If hwRes.StatusCode = HttpStatusCode.OK Then
            Dim srdrResponse As New StreamReader(hwRes.GetResponseStream(), Encoding.UTF8)
            Dim strResponse As String = srdrResponse.ReadToEnd().Trim()

            strResult = strResponse

        End If
    Catch wex As WebException
        strResult = "Error WebException " + wex.Message
    Catch ex As Exception
        strResult = "Error Exception " + ex.Message
    Finally
        hwReq = Nothing
        hwRes = Nothing
    End Try

    Return strResult
End Function

【问题讨论】:

    标签: vb.net asmx


    【解决方案1】:

    我认为你正在努力解决这个问题。

    将 Web 服务作为引用添加到您的应用程序中,然后通过直接代码访问它比尝试自己构建请求/响应机制要容易得多。我已验证您上面的 URL 可用于在 .Net 项目中创建 Web 引用。

    有关如何添加和使用这些 Web 服务引用的详细信息,请参阅以下 MSDN 文章:

    How to: Add and Remove Web References

    How to: Call a Web Service

    您将从第二篇文章中看到,一旦添加了 Web 服务引用,与之交互就非常容易:

        Dim oService As New gsCash.gsprocess
    
        Dim oRequest As New gsCash.PayWithGscash
        oRequest.gsLogin = "login"
        oRequest.gsPassword = "password"
    
        Dim oResult As gsCash.GsServiceOutput
    
        oResult = oService.PayWithGscash(oRequest)
    

    【讨论】:

    • 感谢您的出色回答,如何显示 oResult 以便我可以读取数据?
    • 如果您在调试模式下单步执行应用程序,您应该能够在调用完成后立即看到 oResult 的内容。在设计时,您还应该能够键入 oResult。并查看 oResult 上可用的属性列表(例如 errDesc 等)。
    猜你喜欢
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2014-04-18
    相关资源
    最近更新 更多