2011-06-18 18 views
0

私はその要求ヘッダーを使用してclosure-compiler.appspot.comにデータをポストしようとしています。HttpWebRequestを生成していますか? ArgumentExceptionエラーが発生しました

これはアプリケーションのリクエストヘッダです。私は.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 

をこのリクエストを作成するには、どのように私は、アプリケーションは私が送信したいヘッダを生成することができなかった何ザッツ

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 

+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」タイプの例外をスローした「System.NotSupportedException」 「reqStr.Positionは、」あなたがreqStrをアクセスもない返事 –

+0

は「System.NotSupportedException」 感謝のタイプの例外をスローしました。長さ? – ChrFin

+0

ストリーム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

これは魅力のようにも機能します。どうもありがとうございました。 –

関連する問題