HTTPS文件上传 [英] HTTPS File Upload
问题描述
我试图通过HTTPS协议将test.txt文件上传到远程服务器。
对于按钮单击,我按如下方式编写代码:
受保护的子btnUploadFile_Click(ByVal sender As Object,ByVal e As EventArgs)处理btnUploadFile.Click
UploadFile(" c:\test.txt"," https://remotemachine.com/test/files/")
结束子
公共子上传文件(ByVal localFile As String,ByVal uploadUrl As String)
试试
Dim req As HttpWebRequest
req = CType(WebRequest.Create(uploadUrl),HttpWebRequest)
req.KeepAlive = True
req.Credentials = New NetworkCredential(" userName"," password")
req.ContentType =" Content-Type:multipart / form-data ; boundary = BOUNDRY" req.Method =" POST"
req.AllowWriteStreamBuffering = True
Dim reqStream As Stream
reqStream = req.GetRequestStream()
Dim rdr As New FileStream(localFile,FileMode.Open)
Dim inData(4096)As Byte
Dim bytesRead = rdr。 Read(inData,0,inData.Length - 1)
while bytesRead> 0
reqStream.Write(inData,0,bytesRead)
bytesRead = rdr.Read(inData,0,inData.Length - 1)
结束时
转发.Close()
reqStream.Close()
Dim objWebResponse As System.Net.HttpWebResponse
objWebResponse = CType(req.GetResponse,HttpWebResponse)
Catch ex As WebException
MsgBox(&"错误信息:" + ex.Message)
最后结束尝试
结束子
objWebResponse = CType(req.GetResponse,HttpWebResponse)上引发异常,并带有消息:远程服务器返回错误:(405)方法不允许
任何人都可以告诉我如何克服这个问题。
Hi,
I am trying to upload test.txt file to remote server through HTTPS protocol.
for that on button click I have write the code as follows:
Protected Sub btnUploadFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUploadFile.Click
UploadFile("c:\test.txt", "https://remotemachine.com/test/files/")
End Sub
Public Sub UploadFile(ByVal localFile As String, ByVal uploadUrl As String)
Try
Dim req As HttpWebRequest
req = CType(WebRequest.Create(uploadUrl), HttpWebRequest)
req.KeepAlive = True
req.Credentials = New NetworkCredential("userName", "password")
req.ContentType = "Content-Type: multipart/form-data; boundary=BOUNDRY"
req.Method = "POST"
req.AllowWriteStreamBuffering = True
Dim reqStream As Stream
reqStream = req.GetRequestStream()
Dim rdr As New FileStream(localFile, FileMode.Open)
Dim inData(4096) As Byte
Dim bytesRead = rdr.Read(inData, 0, inData.Length - 1)
While bytesRead > 0
reqStream.Write(inData, 0, bytesRead)
bytesRead = rdr.Read(inData, 0, inData.Length - 1)
End While
rdr.Close()
reqStream.Close()
Dim objWebResponse As System.Net.HttpWebResponse
objWebResponse = CType(req.GetResponse, HttpWebResponse)
Catch ex As WebException
MsgBox("Error Message:" + ex.Message)
Finally
End Try
End Sub
But it raises an exception at
objWebResponse = CType(req.GetResponse, HttpWebResponse)
with message : The remote server returned an error : (405) Method Not Allowed
Can anyone please let me know as to how can i overcome this issue.
推荐答案
W hat是您要将文件发布到的端点Uri?该端点必须支持POST / PUT方法,否则服务器将拒绝您的请求。
What is the endpoint Uri to which you are posting the file? That endpoint has to support the POST/PUT method, otherwise the server will deny your request.
这篇关于HTTPS文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!