Skip to content

Commit

Permalink
Fixes BingWeb query (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
iosebyte authored Aug 27, 2021
1 parent 809d26e commit 754453a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
16 changes: 9 additions & 7 deletions SearcherCore/SearcherCore/Searcher/BingWebSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private int GetBingResults(string searchString, int currentResultPerPage, int cu
searchString += string.Format(" loc:{0}", RegionToHtmlOption(LocatedInRegion));
OnSearcherLogEvent(new EventsThreads.ThreadStringEventArgs(string.Format("[{0}] Searching first={2} q={1}", Name, searchString, currentOffset + 1)));

string requestUrl = String.Format("http://www.bing.com/search?first={1}&q={0}", searchString, currentOffset + 1);
string requestUrl = String.Format("https://www.bing.com/search?first={1}&q={0}", searchString, currentOffset + 1);

int retries = 0;
bool error;
Expand All @@ -48,14 +48,16 @@ private int GetBingResults(string searchString, int currentResultPerPage, int cu
{
error = false;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
if (!string.IsNullOrEmpty(UserAgent))
if (!String.IsNullOrEmpty(UserAgent))
{
request.UserAgent = UserAgent;

}
else
{
request.UserAgent = DefaultUserAgent;
}
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
request.Timeout = 5000 + 10000 * retries;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Cookie("SRCHHPGUSR", "ADLT=OFF&NRSLT=" + currentResultPerPage, "/", ".bing.com"));

request.CookieContainer.Add(new Cookie("MUID", "00000000000000000000000000000000", "/", ".bing.com"));
try
{
OnSearcherLogEvent(new EventsThreads.ThreadStringEventArgs(string.Format("[{0}] Requesting URL {1}", this.Name, request.RequestUri.ToString())));
Expand Down
5 changes: 2 additions & 3 deletions SearcherCore/SearcherCore/Searcher/DuckduckgoWebSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace FOCA.Searcher
public class DuckduckgoWebSearcher : LinkSearcher
{
private const int MAX_PAGES = 3;
private const string userAgent = "Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Build/KLP) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30";
private static readonly string[] supportedFileTypes = new string[] { "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx" };

public DuckduckgoWebSearcher() : base("DuckDuckGoWeb", supportedFileTypes)
Expand Down Expand Up @@ -51,7 +50,7 @@ private int Query(string searchTerms, CancellationToken cancelToken)
private string SendInitialRequest(string searchString)
{
HttpWebRequest request = HttpWebRequest.CreateHttp(string.Format("https://duckduckgo.com/html/?q={0}&t=h_", System.Web.HttpUtility.UrlEncode(searchString)));
request.UserAgent = userAgent;
request.UserAgent = DefaultUserAgent;
request.Referer = "https://duckduckgo.com/";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
Expand All @@ -68,7 +67,7 @@ private string MoreResults(string searchString, string postParameters)
request.Headers.Add("Origin: https://duckduckgo.com");
request.Referer = "https://duckduckgo.com/";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = userAgent;
request.UserAgent = DefaultUserAgent;
request.ContentLength = postParameters.Length;
request.Headers.Add("Cache-Control: max-age=0");
request.Headers.Add("Upgrade-Insecure-Requests: 1");
Expand Down
1 change: 1 addition & 0 deletions SearcherCore/SearcherCore/Searcher/LinkSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace FOCA.Searcher
{
public abstract class LinkSearcher : Searcher<string, Uri>
{
public const string DefaultUserAgent= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36";
protected List<string> SupportedExtensions { get; private set; }

public string UserAgent { get; set; }
Expand Down

0 comments on commit 754453a

Please sign in to comment.