In a recent forum post, the question was asked how to make an HttpWebRequest when the site in question uses Compression? For instance, digg.com.
If you attempt to use a normal HttpWebRequest, you will get a TimeOut on the request. Even if you extend the standard length of the time out, you still won't get a response.
But I thought I would create a VB.Net sample of the same operation.
Code:
Public Function GetUrl(ByVal Url As String, ByVal PostData As String, ByVal GZip As Boolean) As String
Dim Http As HttpWebRequest = WebRequest.Create(Url)
If GZip = True Then
Http.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
End If
If Not String.IsNullOrEmpty(PostData) Then
Http.Method = "POST"
Dim lbPostBuffer As Byte() = Encoding.Default.GetBytes(PostData)
Http.ContentLength = lbPostBuffer.Length
Using PostStream As Stream = Http.GetRequestStream()
PostStream.Write(lbPostBuffer, 0, lbPostBuffer.Length)
End Using
End If
Using WebResponse As HttpWebResponse = Http.GetResponse()
Dim responseStream As Stream = WebResponse.GetResponseStream()
If (WebResponse.ContentEncoding.ToLower().Contains("gzip")) Then
responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
ElseIf (WebResponse.ContentEncoding.ToLower().Contains("deflate")) Then
responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
End If
Dim reader As StreamReader = New StreamReader(responseStream, Encoding.Default)
Dim html As String = reader.ReadToEnd()
responseStream.Close()
Return html
End Using
End Function
Attached is a working sample of the above.
To use it, simply type in the URL that you would like to request, click the checkbox to indicate if you want to compress or not, and then hit go.
For example, type http://digg.com in the textbox, check the checkbox and then hit go. The response is almost immediate. If however, you type http://digg.com, don't check the checkbox and then hit go, a exception will be raised (which of course isn't handled, as this is just a simple example )
Hi gep 13
Its not working in my case, giving error " The remote server returned an error: (500) Internal Server Error.
Please help me. My code is
Public Function PostXml(ByVal Url As String, ByVal PostData As String) As String
Try
Dim Http As HttpWebRequest = WebRequest.Create(Url)
Http.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
If Not String.IsNullOrEmpty(PostData) Then
Http.Method = "POST"
Dim lbPostBuffer As Byte() = Encoding.Default.GetBytes(PostData)
Http.ContentLength = lbPostBuffer.Length
Using PostStream As Stream = Http.GetRequestStream()
PostStream.Write(lbPostBuffer, 0, lbPostBuffer.Length)
End Using
End If
Using WebResponse As HttpWebResponse = Http.GetResponse()
Dim responseStream As Stream = WebResponse.GetResponseStream()
If (WebResponse.ContentEncoding.ToLower().Contains("gzip")) Then
responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
ElseIf (WebResponse.ContentEncoding.ToLower().Contains("deflate")) Then
responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
End If
Dim reader As StreamReader = New StreamReader(responseStream, Encoding.Default)
Dim html As String = reader.ReadToEnd()
responseStream.Close()
Return html
End Using
without gzip method below code is working but it taking 150 to 200 second to read response data.
Protected Function PostXml(ByVal url As String, ByVal xml As String) As String
Dim bytes As Byte() = Encoding.UTF8.GetBytes(xml)
Dim strResult As String = String.Empty
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.Method = "POST"
request.ContentLength = bytes.Length
request.ContentType = "text/xml" '; charset=utf-8
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(bytes, 0, bytes.Length)
End Using
Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If response.StatusCode <> HttpStatusCode.OK Then
Dim message As String = [String].Format("POST failed. Received HTTP {0}", response.StatusCode)
Throw New ApplicationException(message)
Else
Dim reader As StreamReader = Nothing
Dim responseStream As Stream = response.GetResponseStream()
reader = New StreamReader(responseStream, Encoding.Default) strResult = reader.ReadToEnd() ' Here taking time
response.Close()
responseStream.Close()
reader.Close()
End If
End Using
End Function