在vb.net

时间:2018-06-05 10:53:48

标签: vb.net wcf http stream response

尝试将图像上传到HTTP虚拟目录,我不断收到此异常:

  

System.dll中发生了'System.Net.WebException'类型的第一次机会异常    在System.Net.HttpWebRequest.GetResponse()

Dim mFileStream As New FileStream("/Image Location/", FileMode.Open)
Dim mRequest As WebRequest = WebRequest.Create("/URL/")
mRequest.Headers.Set("filename", "new name")
mRequest.Proxy = New WebProxy("/URL/", True)
mRequest.Method = "POST"
mRequest.ContentLength = mFileStream.Length
Dim mCredentials As New NetworkCredential
mCredentials.Password = "/pass/"
mCredentials.UserName = "/Login Name/"
mRequest.Credentials = mCredentials
Dim mData(mFileStream.Length - 1) As Byte
mFileStream.Read(mData, 0, mFileStream.Length)
mFileStream.Close()
Using dataStream As Stream = mRequest.GetRequestStream()
    dataStream.Write(mData, 0, mData.Length)
    dataStream.Close()
End Using

Dim mResponse As HttpWebResponse = CType(mRequest.GetResponse(), HttpWebResponse)
mResponse.Close()

经过进一步调查,我发现WebException的原因是:

  

ProtocolError远程服务器返回错误:(405)Method Not Allowed。

1 个答案:

答案 0 :(得分:0)

尽管我尝试了一切,但答案还是很简单的 您必须在url中指定文件名

Dim mRequest As WebRequest = WebRequest.Create("/URL/")

Dim mRequest As WebRequest = WebRequest.Create("/URL/" & FileName & FileExtention)

例如:

  

将mRequest视作WebRequest = WebRequest.Create(“ http://1.1.1.1/niveimage.png”)