コード例 #1
0
        /// <summary>
        /// 执行Http请求,上传二进制流数据(最大超时1h)
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="ins">输入流数据</param>
        /// <param name="method">HTTP请求方式</param>
        /// <returns></returns>
        public static string HttpUpload(string url, Stream ins, HttpMethod method = HttpMethod.PUT)
        {
            string result = null;

            HttpWebReqWrapper.Call(url, method,
                                   // 请求参数设置
                                   (HttpWebRequest req) =>
            {
                //req.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";  // 特定标头不能这么设置
                req.ContentType      = "application/octet-stream";
                req.Timeout          = IO_TIMEOUT;
                req.ReadWriteTimeout = IO_TIMEOUT;
                req.SendChunked      = true;
                using (Stream outstream = req.GetRequestStream())
                {
                    int c         = 1024 * 8;
                    byte[] buffer = new byte[c];
                    int l         = ins.Read(buffer, 0, c);
                    while (l > 0)
                    {
                        outstream.Write(buffer, 0, l);
                        outstream.Flush();
                        l = ins.Read(buffer, 0, c);
                    }
                    outstream.Close();
                }
                return(req);
            },
                                   // 响应结果提取
                                   (HttpWebResponse rep) =>
            {
                using (Stream outstream = rep.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(outstream, Encoding.UTF8);
                    result = reader.ReadToEnd();
                    reader.Close();
                }
                return(rep);
            });
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// 获取服务重定向后的URL地址,便于手动处理重定向
        /// </summary>
        /// <param name="url">请求的服务地址</param>
        /// <param name="method">请求方法</param>
        /// <returns></returns>
        public static string GetRedirectedURL(string url, HttpMethod method)
        {
            try
            {
                string redirectUrl = null;
                HttpWebReqWrapper.Call(url, method,
                                       // 请求参数设置
                                       (HttpWebRequest req) =>
                {
                    //req.Headers[HttpRequestHeader.Authorization] = "token";
                    req.AllowAutoRedirect = false;
                    return(req);
                },
                                       // 响应结果提取
                                       (HttpWebResponse rep) =>
                {
                    int code = (int)rep.StatusCode;
                    if (307 == code || 301 == code)
                    {
                        redirectUrl = rep.Headers[HttpResponseHeader.Location];
                    }
                    return(rep);
                });

                return(redirectUrl);
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                if (ex is WebException)
                {
                    err = ParseHdfsWebException(ex as WebException);
                }
                DebugHelper.Error(ex);
                return(null);
            }
        }
コード例 #3
0
        public static string HttpUpload(string url, Stream ins, long offset, long length,
                                        HttpMethod method = HttpMethod.PUT)
        {
            try
            {
                if (url.StartsWith("https"))
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                string result = null;
                HttpWebReqWrapper.Call(url, method,
                                       // 请求参数设置
                                       (HttpWebRequest req) =>
                {
                    //req.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";  // 特定标头不能这么设置
                    req.ContentType      = "application/octet-stream";
                    req.Timeout          = IO_TIMEOUT;
                    req.ReadWriteTimeout = IO_TIMEOUT;
                    req.SendChunked      = true;
                    using (Stream outstream = req.GetRequestStream())
                    {
                        ins.Seek(offset, SeekOrigin.Begin);
                        long endPos   = offset + length;
                        int c         = 1024 * 8;
                        byte[] buffer = new byte[c];
                        while (ins.Position < endPos)
                        {
                            int l = ins.Read(buffer, 0, c);
                            if (ins.Position > endPos)
                            {
                                l = l - (int)(ins.Position - endPos);
                            }
                            if (l <= 0)
                            {
                                break;
                            }
                            outstream.Write(buffer, 0, l);
                            outstream.Flush();
                        }
                        outstream.Close();
                    }
                    return(req);
                },
                                       // 响应结果提取
                                       (HttpWebResponse rep) =>
                {
                    using (Stream outstream = rep.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(outstream, Encoding.UTF8);
                        result = reader.ReadToEnd();
                        reader.Close();
                    }
                    return(rep);
                });
                return(result);
            }
            catch (WebException webex)
            {
                var    resp = (HttpWebResponse)webex.Response;
                string err  = webex.Message;
                if (null != resp)
                {
                    err = string.Format("{0} {1}", (int)resp.StatusCode, resp.StatusCode);
                }
                DebugHelper.OutLog(err);
                return(null);
            }
            catch (Exception ex)
            {
                DebugHelper.Error(ex);  //DebugHelper.OutLog(ex.Message);
                return(null);
            }
        }