gpt4 book ai didi

asp.net - 处理来自 HttpWebRequest.GetResponse 的错误

转载 作者:行者123 更新时间:2023-12-02 11:32:28 25 4
gpt4 key购买 nike

我在尝试让 SMS API 与 .NET 一起工作( ZeepMobile ,如果你感兴趣的话)时经历了一段荒谬的时光...我已经使用 .NET 几年了,但尽管如此关于社交网络和 API 的内容,我需要稍微了解一下 HttpWebRequest。我是新手,但并不是全新的;我能够毫不费力地将我的网站连接到 Twitter(即,我能够修改某人的代码来为我工作)。

无论如何,他们的 API 的工作方式是发送 SMS 消息,您向他们发送 POST,然后他们回复您。我可以很好地发送它,但每次我这样做时,我都会收到黄色死亡错误页(YEPOD),而不是回显一些有助于找出错误的内容,大意是“远程服务器返回了一个错误”。错误:(400) 错误请求。”这发生在我的线路上:

'...creation of httpwebrequest here...'
Dim myWebResponse As WebResponse
myWebResponse = request.GetResponse() '<--- error line

有没有什么方法可以简单地从服务器接收错误,而不是让网络服务器抛出异常并给我 YEPOD?

或者更好的是,任何人都可以发布他们的 Zeep 代码的工作示例吗? :)

谢谢!

编辑:这是我的整个代码块:

Public Shared Function SendTextMessage(ByVal username As String, _
ByVal txt As String) As String
Dim content As String = "user_id=" + _
username + "&body=" + Current.Server.UrlEncode(txt)

Dim httpDate As String = DateTime.Now.ToString("r")
Dim canonicalString As String = API_KEY & httpDate & content

Dim encoding As New System.Text.UTF8Encoding
Dim hmacSha As New HMACSHA1(encoding.GetBytes(SECRET_ACCESS_KEY))

Dim hash() As Byte = hmacSha.ComputeHash(encoding.GetBytes(canonicalString))
Dim b64 As String = Convert.ToBase64String(hash)

'connect with zeep'
Dim request As HttpWebRequest = CType(WebRequest.Create(_
"https://api.zeepmobile.com/messaging/2008-07-14/send_message"), HttpWebRequest)
request.Method = "POST"
request.ServicePoint.Expect100Continue = False

' set the authorization levels'
request.Headers.Add("Authorization", "Zeep " & API_KEY & ":" & b64)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = content.Length

' set up and write to stream'
Dim reqStream As New StreamWriter(request.GetRequestStream())
reqStream.Write(content)
reqStream.Close()
Dim msg As String = ""
msg = reqStream.ToString

Dim myWebResponse As WebResponse
Dim myResponseStream As Stream
Dim myStreamReader As StreamReader

myWebResponse = request.GetResponse()

myResponseStream = myWebResponse.GetResponseStream()
myStreamReader = New StreamReader(myResponseStream)
msg = myStreamReader.ReadToEnd()
myStreamReader.Close()
myResponseStream.Close()

' Close the WebResponse'
myWebResponse.Close()
Return msg
End Function

最佳答案

试试这个:

Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
'' set up request
Try
Using response = req.GetResponse()
'' success in here
End Using
Catch ex As WebException
Console.WriteLine(ex.Status)
If ex.Response IsNot Nothing Then
'' can use ex.Response.Status, .StatusDescription
If ex.Response.ContentLength <> 0 Then
Using stream = ex.Response.GetResponseStream()
Using reader = New StreamReader(stream)
Console.WriteLine(reader.ReadToEnd())
End Using
End Using
End If
End If
End Try
<小时/>

C#版本

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
// set up request
try {
using (var response = req.GetResponse()) {
// success in here
}
}
catch (WebException ex) {
Console.WriteLine(ex.Status);
if (ex.Response != null) {
// can use ex.Response.Status, .StatusDescription
if (ex.Response.ContentLength != 0) {
using (var stream = ex.Response.GetResponseStream()) {
using (var reader = new StreamReader(stream)) {
Console.WriteLine(reader.ReadToEnd());
}
}
}
}
}
<小时/>

这是您的代码,稍作修改:

Try
'connect with zeep'
Dim request As HttpWebRequest = CType(WebRequest.Create( _
"https://api.zeepmobile.com/messaging/2008-07-14/send_message"), HttpWebRequest)
request.Method = "POST"
request.ServicePoint.Expect100Continue = False

' set the authorization levels'
request.Headers.Add("Authorization", "Zeep " & API_KEY & ":" & b64)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = content.Length

' set up and write to stream'
Using requestStream As Stream = request.GetRequestStream()
Using requestWriter As New StreamWriter(requestStream)
requestWriter.Write(content)
End Using
End Using

Using myWebResponse As WebResponse = request.GetResponse()
Using myResponseStream As Stream = myWebResponse.GetResponseStream()
Using myStreamReader As StreamReader = New StreamReader(myResponseStream)
Return myStreamReader.ReadToEnd()
End Using
End Using
End Using
Catch ex As WebException
Console.WriteLine(ex.Status)
If ex.Response IsNot Nothing Then
'' can use ex.Response.Status, .StatusDescription
If ex.Response.ContentLength <> 0 Then
Using stream = ex.Response.GetResponseStream()
Using reader = New StreamReader(stream)
Console.WriteLine(reader.ReadToEnd())
End Using
End Using
End If
End If
End Try
<小时/>

转储 header :

Dim headers As WebHeaderCollection = request.Headers
' Displays the headers. Works with HttpWebResponse.Headers as well
Debug.WriteLine(headers.ToString())

' And so does this
For Each hdr As String In headers
Dim headerMessage As String = String.Format("{0}: {1}", hdr, headers(hdr))
Debug.WriteLine(headerMessage)
Next

关于asp.net - 处理来自 HttpWebRequest.GetResponse 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1140068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com