2011-06-18 58 views
0

我想通過使用它的請求標題發佈數據到closure-compiler.appspot.com生成HttpWebRequest?一個ArgumentException錯誤發生

這是應用程序的請求標題。

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4 
Cache-Control: max-age=0 
Content-Length: 269 
Content-Type: application/x-www-form-urlencoded 
Host: closure-compiler.appspot.com 
Origin: null 
Connection: keep-alive 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko)  Chrome/14.0.794.0 Safari/535.1 

那是我做了什麼來創建.NET

申請
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://closure-compiler.appspot.com/compile"); 
req.Connection = "keep-alive"; 
req.Headers.Add("Cache-Control", "max-age=0"); 
req.Headers.Add("Origin","null"); 
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.794.0 Safari/535.1"; 
req.ContentType = "application/x-www-form-urlencoded"; 
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch"); 
req.Headers.Add("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4"); 
req.Headers.Add("Accept-Charset", " ISO-8859-9,utf-8;q=0.7,*;q=0.3"); 
req.Method = "POST"; 

Stream reqStr = req.GetRequestStream(); 
//this line give me an argumentexception 
//Keep-Alive and Close may not be set using this property. 
//Parameter name: value 

所以,我怎麼能生產出應用程序要我送的頭?

+1

@Bans,你無法將所有像使用文本的標題 - 其中一些需要您專門用於該目的的屬性。閱讀關於'HttpWebRequest'的MSDN文檔。 – bzlm

回答

1

如果我讀THIS正確,你只需要設置的ContentType,所以儘量只做到以下幾點:

static void Main(string[] args) 
{ 
    MemoryStream wStr = new MemoryStream(); 
    StreamWriter writer = new StreamWriter(wStr, Encoding.Default); 
    writer.Write("js_code=" + HttpUtility.UrlEncode("alert('hello');// This comment should be stripped")); 
    writer.Write("&compilation_level=" + HttpUtility.UrlEncode("WHITESPACE_ONLY")); 
    writer.Write("&output_format=" + HttpUtility.UrlEncode("text")); 
    writer.Write("&output_info=" + HttpUtility.UrlEncode("compiled_code")); 
    writer.Flush(); 

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://closure-compiler.appspot.com/compile"); 
    req.ContentType = "application/x-www-form-urlencoded"; 
    req.Method = "POST"; 
    req.ContentLength = wStr.Length; 

    Stream reqStr = req.GetRequestStream(); 
    reqStr.Write(wStr.ToArray(), 0, (int)wStr.Length); 

    WebResponse res = req.GetResponse(); 
    Stream response = res.GetResponseStream(); 
    StreamReader reader = new StreamReader(response, Encoding.Default); 

    Console.WriteLine(reader.ReadToEnd()); 
    Console.ReadLine(); 
} 

只用正確的順序工作對我來說(req.ContentLength需要進行設置前寫作)和編碼!

+0

它說: 「reqStr.Length」扔類型的異常你在哪裏存取權限reqStr的「System.NotSupportedException」 「reqStr.Position」扔「System.NotSupportedException」類型 感謝答覆 –

+0

的異常。長度? – ChrFin

+0

Stream reqStr = req.GetRequestStream();這部分拋出我的例外 –

2

如何使用WebClient。它會使你的代碼更具可讀性和除了這個,你不需要用正確的URL編碼您的參數,收盤溪流圍繞,掙扎...:

class Program 
{ 
    static void Main() 
    { 
     using (var client = new WebClient()) 
     { 
      var values = new NameValueCollection 
      { 
       { "js_code", "var a = function(arg) { alert(arg); }; a('abc');" }, 
       { "compilation_level", "ADVANCED_OPTIMIZATIONS" }, 
       { "output_format", "text" }, 
       { "output_info", "compiled_code" }, 
      }; 
      var url = "http://closure-compiler.appspot.com/compile"; 
      var result = client.UploadValues(url, values); 
      Console.WriteLine(Encoding.UTF8.GetString(result)); 
      // outputs 'alert("abc");' on the screen 
     } 
    } 
} 
+0

這也像一個魅力。非常感謝你。 –