Я создал консольное приложение на C #, которое будет загружать html-контент с сайта, анализировать html-содержимое, чтобы получить счетчик тега img на основе src, и опубликовать данные формы обратно.
Когда я делаю это, сервер отвечает неправильным ответом. Я не понимаю, что может пойти не так. Я считаю тэг img вручную, отображая содержание html и совпадения ответов.
Мне было интересно, если он должен иметь все заголовки по запросу?
Не могли бы вы, эксперты, помочь мне разобраться в этом.
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
public class Program {
public static void Main(string[] args) {
var client = new WebClient();
var html = client.DownloadString("//URI- comes- here");
Console.WriteLine(html);
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
int count = htmlDoc.DocumentNode.SelectNodes("//img[@src='filledO.gif']").Count;
Console.WriteLine(count);
string postData = String.Format("title={0}&jobref={1}&valuee={2}", "submit", "P0162", count);
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add(HttpRequestHeader.Cookie, "PHPSESSID=dl1il0k58qb6ebn08bsv7nns36;");
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36");
client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
client.Headers.Add("Accept-Language", "en-US,en;q=0.9");
// Upload the input string using the HTTP 1.0 POST method.
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postData);
byte[] byteResult = client.UploadData("//URI- comes- here", "POST", byteArray);
// Decode and display the result.
Console.WriteLine("\nResult received was {0}", Encoding.ASCII.GetString(byteResult));
//PostContent("//URI- comes- here", count); // calling the function to post the form data to the dataprocessors server
}
private static void PostContent(string urlAddr, int filledCircleCount) {
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(urlAddr);
var postData = "title=submit";
postData += "&ref=P0162";
postData += "&valuee=" + filledCircleCount;
var data = Encoding.ASCII.GetBytes(postData);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.Headers["Cookie"] = "_ga=GA1.3.1376683341.1556868600; PHPSESSID=dl1il0k58qb6ebn08bsv7nns36; pc3o=d2r22";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36";
request.Accept = "*/*";
request.Referer = "uri comes here";
using(Stream stream = request.GetRequestStream()) {
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(responseString);
response.Close();
}
}
}
**The Html that I get is**
HTTP/1.1 200 OK
Date: Fri, 10 May 2019 11:00:42 GMT
Server: Apache/2.4.10 (Amazon) PHP/5.5.17
X-Powered-By: PHP/5.5.17
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 6229
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
<html>
<head>
<title>Answer the question</title>
</head>
<body>
<h1>Solve the puzzle</h1>
<form action="" method="POST">
<input type="hidden" name="title" value="submit">
<p>Ref No: <input type="text" name="ref" value="" size="10"></p><p>How
many filled circles do you see:? <br><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><br><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><br><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><br><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><br><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><br><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><img src="unfilledO.gif" height="12" width="12"><img src="filledO.gif" height="12" width="12"><br></p>
<p>Answer: <input type="text" name="valuee" value="" size="10"></p>
<p><input type="Submit" value="Submit">
</form></body>
</html>
The response expected is
Congratulations ....... blah blah blah
whereas I get is
HTTP/1.1 200 OK
Date: Fri, 10 May 2019 10:42:51 GMT
Server: Apache/2.4.10 (Amazon) PHP/5.5.17
X-Powered-By: PHP/5.5.17
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 168
Content-Type: text/html; charset=utf-8
<html>
<head>
<title>Answer the question</title>
</head>
<body>
<h1>Puzzle</h1><p>Sorry wrong answer</p></body>
</html>