我有以下课程:
internal static class WebRequester
{
/// <summary>
/// Sending GET request.
/// </summary>
/// <param name="url">Request Url.</param>
/// <param name="data">Data for request.</param>
/// <param name="userName">user name for log in</param>
/// <param name="password">password for log in</param>
/// <returns>Response body.</returns>
public static string HttpGet(string url, string data, string userName, string password, string contentType = "application/json")
{
string Out = String.Empty;
Uri uri = new Uri(url);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url + (string.IsNullOrEmpty(data) ? "" : "?" + data));
req.ContentType = contentType;
req.Method = "GET";
req.Accept = "Accept: text/html,application/xhtml+xml,application/xml";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
req.KeepAlive = true;
req.CookieContainer = cookieContainer;
AddAuthentication(userName, password, req);
try
{
System.Net.WebResponse resp = req.GetResponse();
using (System.IO.Stream stream = resp.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
{
Out = sr.ReadToEnd();
sr.Close();
}
}
}
catch (ArgumentException ex)
{
Out = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}", ex.Message);
}
catch (WebException ex)
{
Out = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
}
catch (Exception ex)
{
Out = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
}
return Out;
}
private static void AddAuthentication(string userName, string password, HttpWebRequest req)
{
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
{
string base64Credentials = GetEncodedCredentials(userName, password);
req.Headers.Add("Authorization", "Basic " + base64Credentials);
}
}
private static string GetEncodedCredentials(string userName, string password)
{
string mergedCredentials = string.Format("{0}:{1}", userName, password);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}
}
和另一个班级:
public class ErrorVisualizer
{
private const string contentType = "text/html; charset=UTF-8";
public void ShowErrorInVisualizer()
{
string url = "https://www.tenderned.nl/tenderned-web/aankondiging/overzicht/aankondigingenplatform";
string resultsStart = "<ol class=\"results announcements\">";
var pageContent = WebRequester.HttpGet(url, "", "", "", contentType);
var idxResultsStart = pageContent.IndexOf(resultsStart);
}
}
按钮点击事件跟随操作:
var ev = new ErrorVisualizer();
ev.ShowErrorInVisualizer();
然后在mehtod的调试过程中,ShowErrorInVisualizer()idxResultsStart总是大于零,所以这意味着在pageContent中有一个字符串
但是如果要通过文本可视化工具复制pageContent变量,
并将该内容粘贴到记事本中,我将无法在记事本中找到字符串。
所以,我的问题是如何才能看到变量pageContent的真正含义?