开发者

Closure Compiler Service API

开发者 https://www.devze.com 2023-02-06 13:11 出处:网络
Trying to intergrate the closure compiler service into one of my applications and having some issues.

Trying to intergrate the closure compiler service into one of my applications and having some issues.

Error being returned is "(413) Request Entity Too Large." Sounds reasonable but I know for a fact the service accepts files larger then the one I am sending it.

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





_Result = New St开发者_如何学运维ringBuilder
_HttpWebRequest = DirectCast(WebRequest.Create(String.Format(ClosureWebServiceURL, HttpUtility.UrlEncode(_Script))), HttpWebRequest)
_HttpWebRequest.Method = "POST"
_HttpWebRequest.ContentType = "application/x-www-form-urlencoded"
_HttpWebRequest.ContentLength = 0

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

Any ideas?


Move your request data over to the POST's RequestStream instead of using the querystring.

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


    '//Build's a large javascript for testing
    Dim _Script As String = ""
    For I = 1 To 100
        _Script &= "function hello_" & I & "(name) {  alert('Hello, ' + name);}hello('New user');"
    Next

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

    _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


    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


    Trace.WriteLine(_Result)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号