154 lines
5.9 KiB
C#
154 lines
5.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Globalization;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Security;
|
||
using System.Security.Cryptography.X509Certificates;
|
||
using System.Text;
|
||
|
||
namespace RunUpdater.Lib
|
||
{
|
||
public class HttpHelper
|
||
{
|
||
#region 方法 -> 检查网站是否可以访问
|
||
/// <summary>
|
||
/// 检查网站是否可以访问
|
||
/// </summary>
|
||
/// <param name="ConnectUrl"></param>
|
||
/// <returns></returns>
|
||
public static bool UrlIsExist(string ConnectUrl)
|
||
{
|
||
try
|
||
{
|
||
// Creates an HttpWebRequest for the specified URL.
|
||
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ConnectUrl);
|
||
myHttpWebRequest.Timeout = 3 * 1000;
|
||
// 有些网站会阻止程序访问,需要加入下面这句
|
||
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
|
||
myHttpWebRequest.Method = "GET";
|
||
// Sends the HttpWebRequest and waits for a response.
|
||
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
|
||
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
|
||
{
|
||
myHttpWebResponse.Close();
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
myHttpWebResponse.Close();
|
||
return false;
|
||
}
|
||
// Releases the resources of the response.
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
///<summary>
|
||
/// 获取标准北京时间
|
||
///</summary>
|
||
///<returns>
|
||
///<para>成功:返回服务器当前时间</para>
|
||
///<para>失败:返回DateTime.MinValue</para>
|
||
///</returns>
|
||
public static DateTime GetBeijingTime(string timeServerURL, DateTime localDate)
|
||
{
|
||
DateTime date_Server = localDate;
|
||
try
|
||
{
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(timeServerURL);
|
||
request.Timeout = 3 * 1000;
|
||
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
|
||
request.Method = "HEAD";
|
||
request.AllowAutoRedirect = false;
|
||
using (HttpWebResponse reponse = (HttpWebResponse)request.GetResponse())
|
||
{
|
||
if (DateTime.TryParseExact(reponse.GetResponseHeader("date"), "r", new CultureInfo("en-US"),
|
||
DateTimeStyles.None, out DateTime gmtTime))
|
||
{
|
||
date_Server = gmtTime.ToLocalTime(); //GMT要加8个小时才是北京时间
|
||
}
|
||
reponse.Close();
|
||
}
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
if (DateTime.TryParseExact(ex.Response.Headers.Get("Date"), "r", new CultureInfo("en-US"),
|
||
DateTimeStyles.None, out DateTime gmtTime))
|
||
{
|
||
date_Server = gmtTime.ToLocalTime(); //GMT要加8个小时才是北京时间
|
||
}
|
||
}
|
||
return date_Server;
|
||
}
|
||
#region 方法 -> HttpPost
|
||
|
||
/// <summary>
|
||
/// Post请求方法
|
||
/// </summary>
|
||
/// <param name="postData"></param>
|
||
/// <param name="requestURL"></param>
|
||
/// <param name="contentType"></param>
|
||
/// <param name="timeOut"></param>
|
||
/// <returns></returns>
|
||
public static string HttpPost(string postData, string requestURL, string contentType = "application/x-www-form-urlencoded", int timeOut = 0)
|
||
{
|
||
try
|
||
{
|
||
CookieContainer cookieContainer = new CookieContainer();
|
||
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
|
||
// 设置提交的相关参数
|
||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
|
||
if (timeOut == 0)
|
||
{
|
||
timeOut = 5;
|
||
}
|
||
request.Timeout = timeOut * 1000;
|
||
request.Method = "POST";
|
||
request.KeepAlive = false;
|
||
request.ContentType = contentType;
|
||
request.CookieContainer = cookieContainer;
|
||
request.ContentLength = postBytes.Length;
|
||
|
||
using (Stream reqStream = request.GetRequestStream())
|
||
{
|
||
//LogHelper.WriteSendLog("==开始写数据==");
|
||
reqStream.Write(postBytes, 0, postBytes.Length);
|
||
//LogHelper.WriteSendLog("==写入完成==");
|
||
}
|
||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||
{
|
||
//在这里对接收到的页面内容进行处理
|
||
//直到request.GetResponse()程序才开始向目标网页发送post请求
|
||
Stream responseStream = response.GetResponseStream();
|
||
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
|
||
{
|
||
return reader.ReadToEnd();
|
||
}
|
||
}
|
||
}
|
||
catch (WebException wex)
|
||
{
|
||
return wex.ToString();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return ex.Message;
|
||
}
|
||
}
|
||
|
||
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||
{
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
}
|