1、实现post请求

2、实现get请求

3、实现服务器检查ping

4、实现服务器端口开放检查

5、实现URL主机信息(IP)获取

6、实现文件下载

7、WindowsMediaPlayer1播放时间转为秒

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace WindowsVIKI
{
    class HttpHelper
    {
        public static string Post(string Url, string Data, string Referer)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.Referer = Referer;
            byte[] bytes = Encoding.UTF8.GetBytes(Data);
            request.ContentType = "application/json";
            request.ContentLength = bytes.Length;
            Stream myResponseStream = request.GetRequestStream();
            myResponseStream.Write(bytes, 0, bytes.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }
            return retString;
        }

        /// <summary>
        /// 检测服务器端口
        /// </summary>
        /// <param name="str_ip"></param>
        /// <param name="str_port"></param>
        /// <returns></returns>
        public static bool CheckServicePort(string str_ip, string str_port)
        {
            if (!string.IsNullOrEmpty(str_port))
            {
                try
                {
                    IPAddress ip = IPAddress.Parse(str_ip);
                    IPEndPoint point = new IPEndPoint(ip, int.Parse(str_port));
                    TcpClient tcp = new TcpClient();
                    tcp.Connect(point);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }

            return false;
        }

        /// <summary>  
        /// 是否能 Ping 通指定的主机  
        /// </summary>  
        /// <param name="ip">ip 地址或主机名或域名</param>  
        /// <returns>true 通,false 不通</returns>  
        public static bool Ping(string str_ip)
        {
            bool bRet = false;
            try
            {
                Ping pingSend = new Ping();
                PingReply reply = pingSend.Send(str_ip, 1000);
                if (reply.Status == IPStatus.Success)
                    bRet = true;
            }
            catch (Exception)
            {
                bRet = false;
            }
            return bRet;
        }

        /// <summary>
        /// 获取服务器信息、IP和端口
        /// </summary>
        /// <param name="str_url">地址</param>
        /// <returns>返回数组,0 IP,1 端口</returns>
        public static string[] GetServiceIpAndPort(string str_url)
        {

            string[] sArray = str_url.Split(new string[] { "//", "/" }, StringSplitOptions.RemoveEmptyEntries);
            string str_ip_info = sArray[1].ToString();
            string[] s_ipinfo_array = str_ip_info.Split(':');
            return s_ipinfo_array;

        }

        /// <summary>
        /// Http下载文件
        /// </summary>
        public static string HttpDownloadFile(string url, string path)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream responseStream = response.GetResponseStream();

            //创建本地文件写入流
            Stream stream = new FileStream(path, FileMode.Create);

            byte[] bArr = new byte[1024];
            int size = responseStream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                stream.Write(bArr, 0, size);
                size = responseStream.Read(bArr, 0, (int)bArr.Length);
            }
            stream.Close();
            responseStream.Close();
            return path;
        }


        /// <summary>
        /// 播放时间计算
        /// </summary>
        /// <param name="str_time"></param>
        /// <returns></returns>
        public static int play_time2num(string str_time)
        {           
            
            int x = -1;

            if (str_time == "")
            {
                return x;
            }

            if (str_time.IndexOf(":") > 0)
            {
                string[] sArray = str_time.Split(':');
                int b = Convert.ToInt32(sArray[1]);
                int m = Convert.ToInt32(sArray[0]);
                x = m * 60 + b;
            }
            else {
                int b = Convert.ToInt32(str_time);
                x =  b;
            }

            return x;
        }

    }
}