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
I would recommend you simplifying your code and disposing disposable resources such as streams and stream readers. You could also use WebClient:
Using client = New WebClient()
Dim values = New NameValueCollection()
values.Add("username", "pdfuser")
values.Add("password", "pdfuser")
values.Add("docid", "inv12154")
Dim result = client.UploadValues(url, values)
File.WriteAllBytes(filename, result)
End Using
精彩评论