我构建了一个功能,其目的是模拟真实浏览器的客户浏览网站,提交表单并从该网站接收结果。 这是我的代码:
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler))
{
handler.Proxy = new WebProxy("127.0.0.1", 8888); //Allow Fiddler
var response = client.PostAsync(baseAddress, null).Result;
var cookies = response.Headers.GetValues("Set-Cookie").ToList();
var sessionCookie = cookies[0];
sessionCookie = sessionCookie.Split(';')[0];
var sessionId = sessionCookie.Replace("ASP.NET_SessionId=", "");
cookieContainer.Add(baseAddress, new Cookie("ASP.NET_SessionId", sessionId, "/"));
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
client.DefaultRequestHeaders.Add("Referer", "http://");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.8,he;q=0.6");
client.DefaultRequestHeaders.Add("Origin", "http://");
client.DefaultRequestHeaders.Connection.Add("keep-alive");
client.DefaultRequestHeaders.Add("Connection", "open");
var values = new Dictionary<string, string>
{
...
};
var content = new StringContent(JsonConvert.SerializeObject(values, new KeyValuePairConverter()), Encoding.UTF8, "application/json");
response = client.PostAsync("http://", content).Result;
//Thread.Sleep(milliseconds);
client.DefaultRequestHeaders.Accept.Clear();
//IEnumerable<Cookie> responseCookies = cookieContainer.GetCookies(baseAddress).Cast<Cookie>();
//foreach (Cookie cookie in responseCookies)
//{
// cookieContainer.Add(cookie);
//}
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/webp"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/apng"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
***response = client.GetAsync("http://").Result;***
client.DefaultRequestHeaders.Accept.Clear();
}
当我从我的VS 2017以dibug模式运行此代码时 - 它运行完美!发布到我的iis8服务器后,我得到了这个回复:
<HTML>
<head>
<script>
Challenge=;
ChallengeId=;
GenericErrorMessageCookies="Cookies must be enabled in order to view this page.";
</script>
<script>
function test(var1)
{
var var_str=""+Challenge;
var var_arr=var_str.split("");
var LastDig=var_arr.reverse()[0];
var minDig=var_arr.sort()[0];
var subvar1 = (2 * (var_arr[2]))+(var_arr[1]*1);
var subvar2 = (2 * var_arr[2])+var_arr[1];
var my_pow=Math.pow(((var_arr[0]*1)+2),var_arr[1]);
var x=(var1*3+subvar1)*1;
var y=Math.cos(Math.PI*subvar2);
var answer=x*y;
answer-=my_pow*1;
answer+=(minDig*1)-(LastDig*1);
answer=answer+subvar2;
return answer;
}
</script>
<script>
client = null;
if (window.XMLHttpRequest)
{
var client=new XMLHttpRequest();
}
else
{
if (window.ActiveXObject)
{
client = new ActiveXObject('MSXML2.XMLHTTP.3.0');
};
}
if (!((!!client)&&(!!Math.pow)&&(!!Math.cos)&&(!![].sort)&&(!![].reverse)))
{
document.write("Not all needed JavaScript methods are supported.<BR>");
}
else
{
client.onreadystatechange = function()
{
if(client.readyState == 4)
{
var MyCookie=client.getResponseHeader("X-AA-Cookie-Value");
if ((MyCookie == null) || (MyCookie==""))
{
document.write(client.responseText);
return;
}
var cookieName = MyCookie.split('=')[0];
if (document.cookie.indexOf(cookieName)==-1)
{
document.write(GenericErrorMessageCookies);
return;
}
window.location.reload(true);
}
};
y=test(Challenge);
client.open("POST",window.location,true);
client.setRequestHeader('X-AA-Challenge-ID', ChallengeId);
client.setRequestHeader('X-AA-Challenge-Result',y);
client.setRequestHeader('X-AA-Challenge',Challenge);
client.setRequestHeader('Content-Type' , 'text/plain');
client.send();
}
</script>
</head>
<body>
<noscript>JavaScript must be enabled in order to view this page.</noscript>
</body>
</HTML>
到目前为止我做了什么: 1.我使用来自我的localhost和生产服务器的fiddler来比较我的http请求 - 它们是相同的。 我检查了我的iis网站是否设置使用cookies。我没有在服务器上找到cookie文件,而不是localhost来比较它们 - 我没有找到它们,也不知道它是否相关。
你能想到导致原因/解决方案的任何原因吗?