来自 ASP.NET 的 SMS HTTP API 调用速度很慢

发布于 2024-12-08 08:09:25 字数 3804 浏览 1 评论 0原文

对于 Web 应用程序,我正在创建一个可以从应用程序发送 SMS 的 SMS 模块。我的 SMS 提供商有一个可以通过 HTTP 调用的 API。

该 API 是通过 XmlTextReader 调用的,XmlTextReader 仅读取来自 API 的 XML 响应。

现在我的问题是:当我从网页发送短信(基本上只是执行 HTTP 请求)时,需要一分多钟才能收到短信。

但是,当我从 Windows 控制台应用程序运行完全相同的代码时,我的短信会在不到 5 秒的时间内到达。

我对此进行了多次测试,实际上并不是我的短信提供商速度慢。 不知何故,HTTP 请求从 ASP.NET 引擎内部延迟,并且不会直接从控制台应用程序延迟。有解决办法吗?

这就是我调用 HTTP API 的方式:

 Dim strPostString As String = Convert.ToString(muriPostUrl) & mstrPostPath
    If mblnUseSecureConnection Then
        strPostString = Convert.ToString(muriSecurePostUrl) & mstrPostPath
    End If

    Dim strDataString As String = "username=" & mstrUsername
    strDataString += "&" & "password=" & mstrPassword
    strDataString += "&" & "originator=" & mstrOriginator
    strDataString += "&" & "recipients=" & mstrRecipients
    strDataString += "&" & "gateway=" & mstrGateway
    strDataString += "&" & "reference=" & mstrReference
    strDataString += "&" & "message=" & mstrMessage

    If mstrType <> String.Empty Then
        strDataString += "&" & "type=" & mstrType
    End If

    If mstrUDH <> String.Empty Then
        strDataString += "&" & "udh=" & mstrUDH
    End If

    If Not mdtDeliveryDate = Nothing Then
        Dim objDeliveryDate As New StringBuilder()
        objDeliveryDate.Append(mdtDeliveryDate.Year)
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Month.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Day.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Hour.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Minute.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Second.ToString(), 2))
        strDataString += "&" & "deliverydate=" & Convert.ToString(objDeliveryDate)
    End If

    Dim strReturnValue As String = ""

    Dim xmlReader As New XmlTextReader(String.Format("{0}?{1}", strPostString, strDataString))

    While xmlReader.Read()
        If xmlReader.NodeType = XmlNodeType.Element Then
            Select Case xmlReader.LocalName
                Case "recipients"
                    If True Then
                        mintSuccessCount = Integer.Parse(xmlReader.ReadString())
                        strReturnValue += "recipients : " & mintSuccessCount.ToString() & vbCr & vbLf
                        Exit Select
                    End If
                Case "success"
                    If True Then
                        mblnSuccess = Boolean.Parse(xmlReader.ReadString())
                        strReturnValue += "success : " & mblnSuccess.ToString() & vbCr & vbLf
                        Exit Select
                    End If
                Case "resultcode"
                    If True Then
                        mintResultCode = Integer.Parse(xmlReader.ReadString())
                        strReturnValue += "resultcode : " & mintResultCode.ToString() & vbCr & vbLf
                        Exit Select
                    End If
                Case "resultmessage"
                    If True Then
                        mstrResultMessage = xmlReader.ReadString()
                        strReturnValue += "resultmessage : " & mstrResultMessage & vbCr & vbLf
                        Exit Select
                    End If
            End Select
        End If
    End While

此外,当从 ASP.NET 执行 HTTP 请求时,它不会出现在 Fiddler,而实际的 HTTP 请求发生。如何从 ASP.NET 引擎内跟踪 HTTP fastback?

我仍然不知道问题是什么,但我通过直接通过 HttpWebRequest 进行回发来解决它。我认为问题出在 while 循环中的某个地方。

For a web application, I am creating a SMS module that can send an SMS from the application. My SMS provider has an API that I can call over HTTP.

The API is called via an XmlTextReader who just reads the XML response from the API.

Now my problem is: When I send the SMS from a web page (basically just doing an HTTP request) it takes more than a minute before I receive the SMS.

But when I run the exact same code from a Windows console application, my SMS arrives in less then 5 seconds.

I have done multiple tests with this, and it's not really my SMS provider who is slow.
Somehow, the HTTP request gets delayed from within the ASP.NET engine, and it does not delay directly from an console application. Is there a solution?

This is the way I'm calling the HTTP API:

 Dim strPostString As String = Convert.ToString(muriPostUrl) & mstrPostPath
    If mblnUseSecureConnection Then
        strPostString = Convert.ToString(muriSecurePostUrl) & mstrPostPath
    End If

    Dim strDataString As String = "username=" & mstrUsername
    strDataString += "&" & "password=" & mstrPassword
    strDataString += "&" & "originator=" & mstrOriginator
    strDataString += "&" & "recipients=" & mstrRecipients
    strDataString += "&" & "gateway=" & mstrGateway
    strDataString += "&" & "reference=" & mstrReference
    strDataString += "&" & "message=" & mstrMessage

    If mstrType <> String.Empty Then
        strDataString += "&" & "type=" & mstrType
    End If

    If mstrUDH <> String.Empty Then
        strDataString += "&" & "udh=" & mstrUDH
    End If

    If Not mdtDeliveryDate = Nothing Then
        Dim objDeliveryDate As New StringBuilder()
        objDeliveryDate.Append(mdtDeliveryDate.Year)
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Month.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Day.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Hour.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Minute.ToString(), 2))
        objDeliveryDate.Append(Prefix(mdtDeliveryDate.Second.ToString(), 2))
        strDataString += "&" & "deliverydate=" & Convert.ToString(objDeliveryDate)
    End If

    Dim strReturnValue As String = ""

    Dim xmlReader As New XmlTextReader(String.Format("{0}?{1}", strPostString, strDataString))

    While xmlReader.Read()
        If xmlReader.NodeType = XmlNodeType.Element Then
            Select Case xmlReader.LocalName
                Case "recipients"
                    If True Then
                        mintSuccessCount = Integer.Parse(xmlReader.ReadString())
                        strReturnValue += "recipients : " & mintSuccessCount.ToString() & vbCr & vbLf
                        Exit Select
                    End If
                Case "success"
                    If True Then
                        mblnSuccess = Boolean.Parse(xmlReader.ReadString())
                        strReturnValue += "success : " & mblnSuccess.ToString() & vbCr & vbLf
                        Exit Select
                    End If
                Case "resultcode"
                    If True Then
                        mintResultCode = Integer.Parse(xmlReader.ReadString())
                        strReturnValue += "resultcode : " & mintResultCode.ToString() & vbCr & vbLf
                        Exit Select
                    End If
                Case "resultmessage"
                    If True Then
                        mstrResultMessage = xmlReader.ReadString()
                        strReturnValue += "resultmessage : " & mstrResultMessage & vbCr & vbLf
                        Exit Select
                    End If
            End Select
        End If
    End While

Also, when performing an HTTP request from ASP.NET, it does not appear in Fiddler, while the actual HTTP request happens. How can I track the HTTP fastback from within the ASP.NET engine?

I still don't know what the problem is, but I worked around it by doing the postback via an HttpWebRequest directly. I think the problem was somewhere in the while loop.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文