本文整理汇总了VB.NET中System.Net.FileWebRequest类的典型用法代码示例。如果您正苦于以下问题:VB.NET FileWebRequest类的具体用法?VB.NET FileWebRequest怎么用?VB.NET FileWebRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileWebRequest类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: showUsage
'
' This example creates or opens a text file and stores a string in it.
' Both the file and the string are passed by the user.
' Note. For this program to work, the folder containing the test file
' must be shared, with its permissions set to allow write access.
Imports System.Net
Imports System.IO
Imports System.Text
Namespace Mssc.PluggableProtocols.File
Module TestGetRequestStream
Class TestGetRequestStream
Private Shared myFileWebRequest As FileWebRequest
' Show how to use this program.
Private Shared Sub showUsage()
Console.WriteLine(ControlChars.Lf + "Please enter file name and timeout :")
Console.WriteLine("Usage: vb_getrequeststream <systemname>/<sharedfoldername>/<filename> timeout")
Console.WriteLine("Example: vb_getrequeststream ngetrequestrtream() ndpue/temp/hello.txt 1000")
Console.WriteLine("Small time-out values (for example, 3 or less) cause a time-out exception.")
End Sub
Private Shared Sub makeFileRequest(ByVal fileName As String, ByVal timeout As Integer)
Try
' Create a Uri object.to access the file requested by the user.
Dim myUrl As New Uri("file://" + fileName)
' Create a FileWebRequest object.for the requeste file.
myFileWebRequest = CType(WebRequest.CreateDefault(myUrl), FileWebRequest)
' Set the time-out to the value selected by the user.
myFileWebRequest.Timeout = timeout
' Set the Method property to POST
myFileWebRequest.Method = "POST"
Catch e As WebException
Console.WriteLine(("WebException is: " + e.Message))
Catch e As UriFormatException
Console.WriteLine(("UriFormatWebException is: " + e.Message))
End Try
End Sub
Private Shared Sub writeToFile()
Try
' Enter the string to write to the file.
Console.WriteLine("Enter the string you want to write:")
Dim userInput As String = Console.ReadLine()
' Convert the string to a byte array.
Dim encoder As New ASCIIEncoding
Dim byteArray As Byte() = encoder.GetBytes(userInput)
' Set the ContentLength property.
myFileWebRequest.ContentLength = byteArray.Length
Dim contentLength As String = myFileWebRequest.ContentLength.ToString()
Console.WriteLine(ControlChars.Lf + "The content length is {0}.", contentLength)
' Get the file stream handler to write to the file.
Dim readStream As Stream = myFileWebRequest.GetRequestStream()
' Write to the stream.
' Note. For this to work the file must be accessible
' on the network. This can be accomplished by setting the property
' sharing of the folder containg the file.
' FileWebRequest.Credentials property cannot be used for this purpose.
readStream.Write(byteArray, 0, userInput.Length)
Console.WriteLine(ControlChars.Lf + "The String you entered was successfully written to the file.")
readStream.Close()
Catch e As WebException
Console.WriteLine(("WebException is: " + e.Message))
Catch e As UriFormatException
Console.WriteLine(("UriFormatWebException is: " + e.Message))
End Try
End Sub
Public Shared Sub Main(ByVal args() As String)
If args.Length < 2 Then
showUsage()
Else
makeFileRequest(args(0), Integer.Parse(args(1)))
writeToFile()
End If
End Sub
End Class
End Module
End Namespace