C# 发起Post请求时,如果修改了header的referer参数,运行就会报错:
for (int i = 0; i<textBoxHeader.Lines.Length; i++)
{
arr[i] = textBoxHeader.Lines[i];
string autoKey = arr[i].Split(':')[0];
string autoValue = arr[i].Replace(autoKey + ":", string.Empty);
req.Headers.Add(autoKey, autoValue);
}
经过查找发现,针对ContentType、UserAgent、Accept、Referer、Connection等不能直接加入Headers,需要采用,如下表格,进行处理。
因此,我们对如上代码进行修改。
for (int i = 0; i<textBoxHeader.Lines.Length; i++)
{
arr[i] = textBoxHeader.Lines[i];
string autoKey = arr[i].Split(':')[0];
string autoValue = arr[i].Replace(autoKey + ":", string.Empty);
//自定义header
if (autoKey == "Content-Type")
{
req.ContentType = autoValue;
}
else if (autoKey == "User-Agent")
{
req.UserAgent = autoValue;
}
else if (autoKey == "Accept")
{
req.Accept = autoValue;
}
else if (autoKey == "Referer")
{
req.Referer = autoValue;
}
else if (autoKey == "Connection")
{
break;
}
else
{
req.Headers.Add(autoKey, autoValue);
}
}
这样,在抓包的时候,我们就可以抓取到自定义headers的信息了。
参考文献
- https://www.yesdotnet.com/archive/post/1619107858.html