1

クロージャコンパイラサービスを私のアプリケーションの1つに統合しようとしていますが、いくつかの問題があります。

返されるエラーは「(413)リクエストエンティティが大きすぎます」です。合理的に聞こえますが、実際には、サービスは送信しているファイルよりも大きいファイルを受け入れます。

    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 StringBuilder
_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

何か案は?

4

1 に答える 1

2

RequestStreamクエリ文字列を使用する代わりに、リクエストデータをPOSTに移動します。

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)
于 2011-01-21T14:13:14.210 に答える