0

I am trying to post data to closure-compiler.appspot.com by using its request header.

This is the request header of application.

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 

Thats what I did to create this request in .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 

So how can I produce the header which the application wants me to send ?

1
  • 1
    @Bans, You can't set all headers like that using text - some of them require you to use properties specifically for the purpose. Go read the MSDN documentation on HttpWebRequest. Commented Jun 18, 2011 at 16:21

2 Answers 2

2

How about using a WebClient. It will make your code much more readable and in addition to this you don't need to struggle with properly URL encoding your parameters, closing streams around, ...:

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 } } } 
Sign up to request clarification or add additional context in comments.

Comments

1

If I read THIS correctly you only need to set the ContentType, so try to do only the following:

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(); } 

It only worked for me with the correct order (req.ContentLength needs to be set before writing) and encoding!

3 Comments

It says: 'reqStr.Length' threw an exception of type 'System.NotSupportedException' 'reqStr.Position' threw an exception of type 'System.NotSupportedException' Thanks for reply
Where do you acces reqStr.Length?
Stream reqStr = req.GetRequestStream(); This part throws me that exception

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.