如何解决产生HttpWebRequest吗?发生ArgumentException错误
| 我正在尝试使用其请求标头将数据发布到“ 0”。 这是应用程序的请求标头。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,*/*;q=0.8\";
req.Headers.Add(\"Accept-Encoding\",\"gzip,sdch\");
req.Headers.Add(\"Accept-Language\",\"tr-TR,en;q=0.4\");
req.Headers.Add(\"Accept-Charset\",\" ISO-8859-9,*;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
那么,如何生成应用程序要我发送的标头?
解决方法
如果我正确阅读了此内容,则只需要设置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(),(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)和编码!
, 如何使用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
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。