簡體   English   中英

VB.NET HTTPS WEBREQUEST

[英]VB.NET HTTPS WEBREQUEST

我有一個使用httpwebrequest發布文本文件的VB.NET代碼。 將URL從http://更改為https://后,該代碼將不再起作用,並且將生成以下消息:“遠程服務器返回了錯誤:(407)需要代理身份驗證。”

請注意,我嘗試使用POSTMAN(在Google chrome上)手動發布文件,並且在不要求代理身份驗證的情況下也可以正常工作。 為什么僅當我嘗試使用vb.net代碼發布文本文件時才生成此錯誤消息,我該如何解決?

這是我的代碼:

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

終端模塊

可能需要搶先驗證。 如果您不了解文檔中的參數,建議您安裝諸如fiddler之類的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM