Google Closure编译REST Api突然引发错误405“方法不允许”

Jam*_*oll 3 post httpwebrequest google-closure-compiler

我已经使用Closure作为y应用程序的一部分来压缩Javascript了一段时间,但是刚开始出现错误 405 - Method not allowed

我下面的代码已经工作了几年,但是现在已经停止了。

注意:仅当我的应用程序中的Javascript文件中检测到任何更改时,才经常调用此方法。

除了此,我从Closure应用程序中没有得到更多的错误信息。

显然,此代码执行POST操作。如果我使用在https://closure-compiler.appspot.com/home上找到的表单,它可以工作,但是如果我浏览到URL或使用我的代码得到Error 405,则几乎就像我的代码正在尝试一种GET方法。 ..但不是...

有任何想法吗?

Public Class Closure

    Property OriginalCode As String
    Property CompiledCode As String
    Property Errors As ArrayList
    Property Warnings As ArrayList
    Property Statistics As Dictionary(Of String, Object)

    Public Sub New(Input As String, Optional CompliationLevel As String = "SIMPLE_OPTIMIZATIONS")

        Dim _HttpWebRequest As HttpWebRequest
        Dim _Result As StringBuilder
        Dim ClosureWebServiceURL As String = "http://closure-compiler.appspot.com/compile?"
        Dim ClosureWebServicePOSTData As String = "output_format=json&output_info=compiled_code" &
                                                                        "&output_info=warnings" &
                                                                        "&output_info=errors" &
                                                                        "&output_info=statistics" &
                                                                        "&compilation_level=" & CompliationLevel & "" &
                                                                        "&warning_level=default" &
                                                                        "&js_code={0}"


        '// Create the POST data
        Dim Data = String.Format(ClosureWebServicePOSTData, HttpUtility.UrlEncode(Input))

        _Result = New StringBuilder
        _HttpWebRequest = DirectCast(WebRequest.Create(ClosureWebServiceURL), HttpWebRequest)
        _HttpWebRequest.Method = "POST"
        _HttpWebRequest.ContentType = "application/x-www-form-urlencoded"
        '//Set the content length to the length of the data. This might need to change if you're using characters that take more than 256 bytes
        _HttpWebRequest.ContentLength = Data.Length
        '//Write the request stream
        Using SW As New StreamWriter(_HttpWebRequest.GetRequestStream())
            SW.Write(Data)
        End Using

        Try

            Dim response As WebResponse = _HttpWebRequest.GetResponse()

            Using responseStream As Stream = response.GetResponseStream
                Dim encoding As Encoding = System.Text.Encoding.GetEncoding("utf-8")
                Using readStream As New StreamReader(responseStream, encoding)
                    Dim read(256) As Char
                    Dim count As Integer = readStream.Read(read, 0, 256)
                    While count > 0
                        Dim str As New String(read, 0, count)
                        _Result.Append(str)
                        count = readStream.Read(read, 0, 256)
                    End While
                End Using
            End Using

            Dim js As New JavaScriptSerializer
            js.MaxJsonLength = Int32.MaxValue

            Dim d As Dictionary(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(_Result.ToString())
            Me.CompiledCode = d.NullKey("compiledCode")
            Me.Warnings = TryCast(d.NullKey("warnings"), ArrayList)
            Me.Errors = TryCast(d.NullKey("errors"), ArrayList)
            Me.Statistics = TryCast(d.NullKey("statistics"), Dictionary(Of String, Object))

        Catch ex As Exception
            Me.CompiledCode = ""
            If Me.Errors Is Nothing Then
                Dim er As New List(Of String)
                er.Add(ex.ToString())
                Me.Errors = New ArrayList(er)
            Else
                Me.Errors.Add(ex.ToString())
            End If
        End Try

        Me.OriginalCode = Input

    End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

Xav*_*ier 5

Closure REST api重定向到https,您可能想要尝试直接POST到“ https://closure-compiler.appspot.com/compile ”,以避免重定向。