[英]VB.NET HTTPS WEBREQUEST
I have a VB.NET code that posts a text file using httpwebrequest. 我有一个使用httpwebrequest发布文本文件的VB.NET代码。 Once the URL was changed from http:// to https:// the code is not working anymore, and the following message is being generated: "The remote server returned an error: (407) Proxy Authentication Required."
将URL从http://更改为https://后,该代码将不再起作用,并且将生成以下消息:“远程服务器返回了错误:(407)需要代理身份验证。”
Note that, I tried to post the file manually using POSTMAN (on google chrome) and it worked just fine without asking for proxy authentication. 请注意,我尝试使用POSTMAN(在Google chrome上)手动发布文件,并且在不要求代理身份验证的情况下也可以正常工作。 Why is this error message generated only when I try to post my text file using the vb.net code and how could I fix it?
为什么仅当我尝试使用vb.net代码发布文本文件时才生成此错误消息,我该如何解决?
Here is my code: 这是我的代码:
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Diagnostics
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Configuration
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Net
Imports System.Threading
Imports System.Security.Cryptography.X509Certificates
Imports System.Net.Security
Module Module1
Sub Main()
Dim dateTime As String = " "
Dim createddate As String = " "
Dim headerbytelength() As Byte
Dim endboundlength() As Byte
Dim e As String = "Test.txt"
dateTime = DateAndTime.Now.ToString()
createddate = DateAndTime.Now.ToString("yyyyMMdd_HHmmssfff")
Dim strRequestURL = ConfigurationManager.AppSettings("strRequestURL").ToString()
Dim strFilename = ConfigurationManager.AppSettings("Path") + e
Dim request As HttpWebRequest = CType(WebRequest.Create(strRequestURL), HttpWebRequest)
request.Method = "POST"
CType(request, HttpWebRequest).UserAgent = ".NET Framework Example Client"
Dim response As HttpWebResponse
Dim JSONResponse As String = ""
Dim writer As TextWriter = New StreamWriter(ConfigurationManager.AppSettings("LogPath"), True)
writer.WriteLine("")
writer.WriteLine("File name: " + e + " Date/Time: " + createddate)
writer.WriteLine("")
Try
System.Threading.Thread.Sleep(ConfigurationManager.AppSettings("Delay"))
Dim boundary As String = IO.Path.GetRandomFileName
Dim header As New System.Text.StringBuilder()
header.AppendLine("--" & boundary)
header.Append("Content-Disposition: form-data; name=""file"";")
header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(strFilename))
header.AppendLine()
header.AppendLine("Content-Type: application/octet-stream")
header.AppendLine()
Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
headerbytelength = System.Text.Encoding.UTF8.GetBytes(header.ToString)
endboundlength = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
request.AllowAutoRedirect = True
request.Timeout = -1
request.KeepAlive = True
request.AllowWriteStreamBuffering = False
request.ContentType = "multipart/form-data; boundary=" & boundary
'request.ContentLength = 0
request.ContentLength = headerbytes.Length + New IO.FileInfo(strFilename).Length + endboundarybytes.Length
Dim s As IO.Stream = request.GetRequestStream
s.Write(headerbytes, 0, headerbytes.Length)
Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(strFilename)
s.Write(filebytes, 0, filebytes.Length)
s.Write(endboundarybytes, 0, endboundarybytes.Length)
s.Close()
response = CType(request.GetResponse(), HttpWebResponse)
Dim receiveStream As Stream = response.GetResponseStream
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Dim jResults As JObject = JObject.Parse(readStream.ReadToEnd)
Dim results As List(Of JToken) = jResults.Children().ToList()
For Each item As JProperty In results
item.CreateReader()
If item.Name = "message" Then
writer.WriteLine(item.Value.ToString)
End If
If item.Name = "status" Then
writer.WriteLine(item.Value.ToString)
End If
If item.Name = "errors" Then
Dim i As Integer = 0
Dim spl() As String = Split(item.Value.ToString, "],")
Do Until i > spl.Length - 1
writer.WriteLine(spl(i))
i = i + 1
Loop
End If
Next
If File.Exists(strFilename) Then
File.Move(strFilename, (ConfigurationManager.AppSettings("HistPath") + createddate + "__" + e).ToString())
writer.WriteLine("BLU File " + e + " was moved to: " + ConfigurationManager.AppSettings("HistPath"))
End If
response.Close()
readStream.Close()
Catch ex As Exception
If ex.Message = "The remote server returned an error: (401) Unauthorized." Then
writer.WriteLine("BLU File " + e + " is empty")
If File.Exists(strFilename) Then
File.Move(strFilename, (ConfigurationManager.AppSettings("HistPath") + e).ToString())
writer.WriteLine("BLU File " + e + " was moved to: " + ConfigurationManager.AppSettings("HistPath"))
End If
Else
writer.WriteLine(ex.ToString)
End If
End Try
writer.WriteLine("------------------------------------------------END------------------------------------------------")
writer.Close()
End Sub
Public Function AcceptAllCertifications(ByVal sender As Object, ByVal certification As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
Return True
End Function
End Module 终端模块
Maybe preemptive authentication is required. 可能需要抢先验证。 I'd recommend installing an app like fiddler if you do not know the parameters from a documentation.
如果您不了解文档中的参数,建议您安装诸如fiddler之类的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.