Trying to use HttpWebRequest to load a page in a file.
Posted
by Malcolm
on Stack Overflow
See other posts from Stack Overflow
or by Malcolm
Published on 2010-04-17T06:21:32Z
Indexed on
2010/04/17
6:33 UTC
Read the original article
Hit count: 284
aspnet
|asp.net-mvc
Hi,
I have a ASP.NET MVC app that works fine in the browser.
I am using the following code to be able to write the html of a retrieved page to a file. (This is to use in a PDF conversion component)
But this code errors out continually but not in the browser.
I get timeout errors sometimes asn 500 errors.
Public Function GetPage(ByVal url As String, ByVal filename As String) As Boolean
Dim request As HttpWebRequest
Dim username As String
Dim password As String
Dim docid As String
Dim poststring As String
Dim bytedata() As Byte
Dim requestStream As Stream
Try
username = "pdfuser"
password = "pdfuser"
docid = "docid=inv12154"
poststring = String.Format("username={0}&password={1}&{2}", username, password, docid)
bytedata = Encoding.UTF8.GetBytes(poststring)
request = WebRequest.Create(url)
request.Method = "Post"
request.ContentLength = bytedata.Length
request.ContentType = "application/x-www-form-urlencoded"
requestStream = request.GetRequestStream()
requestStream.Write(bytedata, 0, bytedata.Length)
requestStream.Close()
request.Timeout = 60000
Dim response As HttpWebResponse
Dim responseStream As Stream
Dim reader As StreamReader
Dim sb As New StringBuilder()
Dim line As String = String.Empty
response = request.GetResponse()
responseStream = response.GetResponseStream()
reader = New StreamReader(responseStream, System.Text.Encoding.ASCII)
line = reader.ReadLine()
While (Not line Is Nothing)
sb.Append(line)
line = reader.ReadLine()
End While
File.WriteAllText(filename, sb.ToString())
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return True
End Function
© Stack Overflow or respective owner