Example #1
0
        /// <summary>
        /// 发送只有url的get请求
        /// </summary>
        /// <param name="RequestUrl">url</param>
        /// <param name="CookieContainer">cookie</param>
        /// <returns>响应报文</returns>
        public static WebResponse HttpGet(this string RequestUrl, CookieContainer CookieContainer = null)
        {
TRY_AGAIN:
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(RequestUrl);
            WebProxy webProxy;

            if (DefaultProxyIp.EnableWebProxy(RequestUrl, out webProxy))
            {
                Req.Proxy = webProxy;
            }
            Req.Method = "GET";
            Req.LoadConfig(RequestUrl);
            if (CookieContainer != null)
            {
                Req.CookieContainer = CookieContainer;
            }
            try
            {
                return(Req.GetResponse());
            }
            catch (Exception ex)
            {
                if (DefaultTryExceptionHandle.CheckTry(RequestUrl, ex))
                {
                    goto TRY_AGAIN;
                }
                else
                {
                    throw ex;
                }
            }
        }
Example #2
0
 /// <summary>
 /// 初始化操作
 /// </summary>
 static HttpWebRequestHelper()
 {
     _httpCfg = new XmlDocument();
     if (File.Exists(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "http.xml"))
     {
         _httpCfg.LoadXml(File.ReadAllText(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "http.xml"));
     }
     DefaultProxyIp            = new DefaultProxyIp();
     DefaultCheckLogout        = new DefaultCheckLogout();
     DefaultTryExceptionHandle = new DefaultTryExceptionHandle();
     PoolVersionMap            = new Dictionary <string, string>();
 }
Example #3
0
        /// <summary>
        /// 发送只有报文体的post请求
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="Body"></param>
        /// <param name="CookieContainer"></param>
        /// <returns>响应报文</returns>
        public static WebResponse HttpPost(this string RequestUrl, string Body, CookieContainer CookieContainer = null)
        {
TRY_AGAIN:
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(RequestUrl);
            WebProxy webProxy;

            if (DefaultProxyIp.EnableWebProxy(RequestUrl, out webProxy))
            {
                Req.Proxy = webProxy;
            }
            Req.Method      = "POST";
            Req.ContentType = "application/json";
            Req.LoadConfig(RequestUrl);
            if (CookieContainer != null)
            {
                Req.CookieContainer = CookieContainer;
            }
            using (Stream stream = Req.GetRequestStream())
            {
                byte[] b = RequestUrl.LoadRequestEncoding().GetBytes(Body);
                stream.Write(b, 0, b.Length);
            }
            try
            {
                return(Req.GetResponse());
            }
            catch (Exception ex)
            {
                if (DefaultTryExceptionHandle.CheckTry(RequestUrl, ex))
                {
                    goto TRY_AGAIN;
                }
                else
                {
                    throw ex;
                }
            }
        }
Example #4
0
        /// <summary>
        /// 发送只含有表单参数的post请求,支持上传文件
        /// </summary>
        /// <param name="RequestUrl">请求的url</param>
        /// <param name="Param">表单参数,上传文件用</param>
        /// <param name="CookieContainer">cookie容器</param>
        /// <returns>响应报文</returns>
        public static WebResponse HttpPost(this string RequestUrl, Dictionary <string, dynamic> Param, CookieContainer CookieContainer = null)
        {
TRY_AGAIN:
            string boundary = "---------------" + DateTime.Now.Ticks.ToString("x");

            byte[] beginBoundaryByte = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
            byte[] endBoundaryByte   = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
            byte[] newLine           = Encoding.ASCII.GetBytes("\r\n");

            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(RequestUrl);
            WebProxy       webProxy;

            if (DefaultProxyIp.EnableWebProxy(RequestUrl, out webProxy))
            {
                Req.Proxy = webProxy;
            }

            Req.Method      = "POST";
            Req.ContentType = "multipart/form-data;boundary=" + boundary;
            Req.LoadConfig(RequestUrl);
            if (CookieContainer != null)
            {
                Req.CookieContainer = CookieContainer;
            }
            byte[] cache = new byte[4096];
            Type   Type;
            string key;

            using (Stream stream = new BufferedStream(Req.GetRequestStream()))
            {
                Encoding reqEncoding = RequestUrl.LoadRequestEncoding();
                foreach (KeyValuePair <string, dynamic> kv in Param)
                {
                    Type = kv.Value.GetType();
                    key  = kv.Key;
                    stream.Write(beginBoundaryByte, 0, beginBoundaryByte.Length);
                    if (typeof(string) == Type)
                    {
                        string value = kv.Value;
                        byte[] temp  = reqEncoding.GetBytes($"Content-Disposition: form-data; name=\"{key}\" \r\n\r\n{value}");
                        stream.Write(temp, 0, temp.Length);
                    }
                    else if (typeof(FileInfo) == Type)
                    {
                        FileInfo f        = kv.Value;
                        string   filename = f.Name;
                        byte[]   temp     = reqEncoding.GetBytes($"Content-Disposition: form-data; name=\"{key}\"; filename=\"{filename}\"\r\nContent-Type: application/octet-stream\r\n\r\n");
                        stream.Write(temp, 0, temp.Length);
                        using (Stream input = new BufferedStream(f.OpenRead()))
                        {
                            for (int len; (len = input.Read(cache, 0, cache.Length)) > 0;)
                            {
                                stream.Write(cache, 0, len);
                            }
                        }
                    }
                    else if (typeof(Stream) == Type)
                    {
                        string filename = key;
                        byte[] temp     = reqEncoding.GetBytes($"Content-Disposition: form-data; name=\"{key}\"; filename=\"{filename}\"\r\nContent-Type: application/octet-stream\r\n\r\n");
                        stream.Write(temp, 0, temp.Length);
                        using (Stream input = kv.Value)
                        {
                            for (int len; (len = input.Read(cache, 0, cache.Length)) > 0;)
                            {
                                stream.Write(cache, 0, len);
                            }
                        }
                    }
                    else if (typeof(Bitmap) == Type)
                    {
                        string filename = key;
                        byte[] temp     = reqEncoding.GetBytes($"Content-Disposition: form-data; name=\"{key}\"; filename=\"{filename}\"\r\nContent-Type: application/octet-stream\r\n\r\n");
                        stream.Write(temp, 0, temp.Length);
                        using (Bitmap Bitmap = kv.Value)
                            Bitmap.Save(stream, ImageFormat.Bmp);
                    }
                    stream.Write(newLine, 0, newLine.Length);
                }
                stream.Write(endBoundaryByte, 0, endBoundaryByte.Length);
            }
            try
            {
                return(Req.GetResponse());
            }
            catch (Exception ex)
            {
                if (DefaultTryExceptionHandle.CheckTry(RequestUrl, ex))
                {
                    goto TRY_AGAIN;
                }
                else
                {
                    throw ex;
                }
            }
        }