-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebReader.cs
More file actions
59 lines (46 loc) · 1.83 KB
/
WebReader.cs
File metadata and controls
59 lines (46 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.IO;
using System.Net;
namespace Crawler
{
public class WebReader
{
private CredentialCache GetCredential(string url,string WebUserName,string WebPassword)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(WebUserName,
WebPassword));
return credentialCache;
}
public string SendHttpReq(string sUrl,string WebUserName,string WebPassword)
{
var encoding = System.Text.Encoding.UTF8;
string lcHtml = null;
try
{
// *** Set properties
var loHttp = (HttpWebRequest)WebRequest.Create(sUrl);
loHttp.Timeout = 5350000;
loHttp.Credentials = GetCredential(sUrl,WebUserName,WebPassword);
loHttp.PreAuthenticate = true;
// *** Retrieve request info headers
using (var loWebResponse = (HttpWebResponse)loHttp.GetResponse())
{
using (var responseStream = loWebResponse.GetResponseStream())
{
using (var loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), encoding))
{
lcHtml = loResponseStream.ReadToEnd();
loResponseStream.Close();
}
}
}
} catch ( Exception ex)
{
Console.WriteLine(ex.Message);
}
return lcHtml;
}
}
}