forked from ParryQiu/Generate-Cnblogs-Articles-To-Markdown
-
Notifications
You must be signed in to change notification settings - Fork 7
/
NetworkHelper.cs
executable file
·121 lines (113 loc) · 4.09 KB
/
NetworkHelper.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Common.Helper
{
public class NetworkHelper
{
public static bool SavePhotoFromUrl(string fileName, string url)
{
var value = false;
try
{
var request = (HttpWebRequest) WebRequest.Create(url);
var response = request.GetResponse();
request.Timeout = 360000;
response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
{
value = SaveBinaryFile(response, fileName);
}
}
catch (Exception err)
{
var aa = err.ToString();
}
return value;
}
public static bool SaveBinaryFile(WebResponse response, string fileName)
{
var value = true;
var buffer = new byte[1024];
try
{
if (File.Exists(fileName))
File.Delete(fileName);
Stream outStream = File.Create(fileName);
var inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
} while (l > 0);
outStream.Close();
inStream.Close();
}
catch
{
value = false;
}
return value;
}
/// <summary>
/// 通过GET方式获取页面的方法
/// </summary>
/// <param name="urlString">请求的URL</param>
/// <param name="encoding">页面编码</param>
/// <returns></returns>
public static string GetHtmlFromGet(string urlString, Encoding encoding)
{
//定义局部变量
HttpWebRequest httpWebRequest;
Stream stream;
string htmlString;
//请求页面
try
{
httpWebRequest = (HttpWebRequest) WebRequest.Create(urlString);
httpWebRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
httpWebRequest.CookieContainer = new CookieContainer();
httpWebRequest.ContentType = "text/html; charset=utf-8";
httpWebRequest.Method = "GET";
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpWebRequest.Timeout = 60000;
httpWebRequest.UserAgent =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36";
}
//处理异常
catch (Exception ex)
{
throw new Exception("GetHtmlFromGet建立页面请求时发生错误!", ex);
}
//获取服务器的返回信息
try
{
var httpWebRespones = (HttpWebResponse) httpWebRequest.GetResponse();
stream = httpWebRespones.GetResponseStream();
}
//处理异常
catch (Exception ex)
{
throw new Exception("GetHtmlFromGet接受服务器返回页面时发生错误!", ex);
}
var streamReader = new StreamReader(stream, encoding);
//读取返回页面
try
{
htmlString = streamReader.ReadToEnd();
}
//处理异常
catch (Exception ex)
{
throw new Exception("GetHtmlFromGet读取页面数据时发生错误!", ex);
}
//释放资源返回结果
streamReader.Close();
stream.Close();
return htmlString;
}
}
}