Ejemplo n.º 1
0
        public HttpRequestResult Request(HttpRequestArgs args)
        {
            HttpWebResponse response      = null;
            Stream          receiveStream = null;
            StreamReader    readStream    = null;

            MemoryStream postStream = new MemoryStream();

            HttpRequestResult result = new HttpRequestResult();

            string uri = args.Url;

            if (String.IsNullOrEmpty(uri))
            {
                result.Exception = new Exception("没有要请求的API URI信息");
                return(result);
            }

            try
            {
                HttpWebRequest request;

                request        = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = args.Method;

                //判断是否使用证书
                if (args.UseCertificate)
                {
                    X509Certificate2 cert = new X509Certificate2(args.CertificatePath, args.CertificatePassword);
                    request.ClientCertificates.Add(cert);
                }

                //直接POST字符串内容,一般是JSON直接发到WEB API上
                if (String.IsNullOrEmpty(args.Content) == false)
                {
                    StreamWriter writer = new StreamWriter(postStream);
                    writer.Write(args.Content);
                    writer.Flush();
                }
                else if (String.IsNullOrEmpty(args.File) == false)
                {
                    RequestWithFile(request, postStream, args.File);
                }

                if (postStream.Length > 0)
                {
                    using (Stream stream = request.GetRequestStream())
                    {
                        postStream.WriteTo(stream);
                    }
                }

                response = (HttpWebResponse)request.GetResponse();

                receiveStream = response.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                readStream = new StreamReader(receiveStream, encode);

                result.Content = readStream.ReadToEnd();
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
                result.Exception = ex;
            }
            finally
            {
                response?.Close();
                receiveStream?.Close();
                readStream?.Close();
                postStream.Close();
            }

            return(result);
        }
Ejemplo n.º 2
0
        public HttpRequestResult Request(HttpRequestArgs args)
        {
            HttpWebResponse response      = null;
            Stream          receiveStream = null;
            StreamReader    readStream    = null;

            HttpRequestResult result = new HttpRequestResult();

            string uri = args.Url;

            if (String.IsNullOrEmpty(uri))
            {
                result.Exception = new Exception("没有要请求的API URI信息");
                return(result);
            }

            try
            {
                HttpWebRequest request;

                request        = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = args.Method;

                if (String.IsNullOrEmpty(args.Content) == false)
                {
                    MemoryStream memoryStream = new MemoryStream();
                    StreamWriter writer       = new StreamWriter(memoryStream);
                    writer.Write(args.Content);
                    writer.Flush();

                    using (Stream stream = request.GetRequestStream())
                    {
                        memoryStream.WriteTo(stream);
                    }
                }
                else if (String.IsNullOrEmpty(args.File) == false)
                {
                    //request.Headers.Add("name", "media");
                    //request.Headers.Add("filename", "a.jpg");
                    request.ContentType = "application/octet-stream";

                    MemoryStream memoryStream = new MemoryStream();
                    //StreamWriter writer = new StreamWriter(memoryStream);

                    // 边界符
                    var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
                    // 边界符
                    var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");

                    const string filePartHeader =
                        "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                        "Content-Type: application/octet-stream\r\n\r\n";
                    var header      = string.Format(filePartHeader, "media", args.File);
                    var headerbytes = Encoding.UTF8.GetBytes(header);

                    memoryStream.Write(beginBoundary, 0, beginBoundary.Length);
                    memoryStream.Write(headerbytes, 0, headerbytes.Length);

                    FileStream fileStream = new FileStream(args.File, FileMode.Open, FileAccess.Read);

                    byte[] buffer    = new byte[1024];
                    int    bytesRead = 0;

                    while (true)
                    {
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        memoryStream.Write(buffer, 0, bytesRead);
                    }

                    memoryStream.Flush();

                    using (Stream stream = request.GetRequestStream())
                    {
                        memoryStream.WriteTo(stream);
                    }
                }

                response = (HttpWebResponse)request.GetResponse();

                receiveStream = response.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                readStream = new StreamReader(receiveStream, encode);

                result.Content = readStream.ReadToEnd();
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (receiveStream != null)
                {
                    receiveStream.Close();
                }
                if (readStream != null)
                {
                    readStream.Close();
                }
            }

            return(result);
        }