Handling Errors from HttpWebRequest.GetResponse

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2009-07-16T20:23:50Z Indexed on 2010/05/12 17:34 UTC
Read the original article Hit count: 655

Filed under:
|
|
|
|

Hey experts -

I'm having a ridiculous time trying to get an SMS API working (ZeepMobile, if you're interested) with .NET... I've been around .NET for a few years, but with all this social networking and API stuff, I need to get into the HttpWebRequest a bit. I'm new at it, but not completely new; I was able to hook up my site to Twitter without too much fuss (ie, I was able to modify someone's code to work for me).

Anyways, the way their API works is to send an SMS message, you send them a POST and they respond back to you. I can send it just fine, but every time I do, rather than echo back something helpful to figure out what the error is, I get the Yellow Error Page Of Death (YEPOD) saying something to the effect of "The remote server returned an error: (400) Bad Request." This occurs on my line:

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

Is there any way to simply receive the error from the server rather than have the webserver throw an exception and give me the YEPOD?

Or better yet, can anyone post a working example of their Zeep code? :)

Thanks!

EDIT: Here's my whole code block:

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

© Stack Overflow or respective owner

Related posts about httpwebrequest

Related posts about zeep