2011-01-21 155 views
1

試圖將閉包編譯器服務整合到我的一個應用程序中,並遇到一些問題。Closure編譯器服務API

返回的錯誤是「(413)Request Entity Too Large」。聽起來很合理,但我知道這個服務接受的文件大於我發送的文件。

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 

任何想法?

回答

2

將您的請求數據移到POST的RequestStream而不是使用查詢字符串。

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

Legand!謝謝你......我應該解決這個問題。要停止工作這麼晚了。 :-) –