首页 > 代码库 > 模拟post或get请求(c u r l),(请求里可以包含图片)

模拟post或get请求(c u r l),(请求里可以包含图片)

/// <summary>
/// Http上传文件类 - HttpWebRequest封装
/// </summary>
public class HttpUploadClient
{
    /// <summary>
    /// 上传执行 方法
    /// </summary>
    /// <param name="parameter">上传文件请求参数</param>
    public static string Execute(UploadParameterType parameter)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            // 1.分界线
            string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")),       // 分界线可以自定义参数
                beginBoundary = string.Format("--{0}\r\n", boundary),
                endBoundary = string.Format("\r\n--{0}--\r\n", boundary);
            byte[] beginBoundaryBytes = parameter.Encoding.GetBytes(beginBoundary),
                endBoundaryBytes = parameter.Encoding.GetBytes(endBoundary);
            // 2.组装开始分界线数据体 到内存流中
            memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
            // 3.组装 上传文件附加携带的参数 到内存流中
            if (parameter.PostParameters != null && parameter.PostParameters.Count > 0)
            {
                foreach (KeyValuePair<stringstring> keyValuePair in parameter.PostParameters)
                {
                    string parameterHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n{2}", keyValuePair.Key, keyValuePair.Value, beginBoundary);
                    byte[] parameterHeaderBytes = parameter.Encoding.GetBytes(parameterHeaderTemplate);
 
                    memoryStream.Write(parameterHeaderBytes, 0, parameterHeaderBytes.Length);
                }
            }
            // 4.组装文件头数据体 到内存流中
            string fileHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n", parameter.FileNameKey, parameter.FileNameValue);
            byte[] fileHeaderBytes = parameter.Encoding.GetBytes(fileHeaderTemplate);
            memoryStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
            // 5.组装文件流 到内存流中
            byte[] buffer = new byte[1024 * 1024 * 1];
            int size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
            while (size > 0)
            {
                memoryStream.Write(buffer, 0, size);
                size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
            }
            // 6.组装结束分界线数据体 到内存流中
            memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            // 7.获取二进制数据
            byte[] postBytes = memoryStream.ToArray();
            // 8.HttpWebRequest 组装
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute));
            webRequest.Method = "POST";
            webRequest.Timeout = 10000;
            webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
            webRequest.ContentLength = postBytes.Length;
            if (Regex.IsMatch(parameter.Url, "^https://"))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
            }
            // 9.写入上传请求数据
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                requestStream.Write(postBytes, 0, postBytes.Length);
                requestStream.Close();
            }
            // 10.获取响应
            using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
            {
                using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), parameter.Encoding))
                {
                    string body = reader.ReadToEnd();
                    reader.Close();
                    return body;
                }
            }
        }
    }
    static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true;
    }
}
 
        /// <summary>
        /// 上传文件 - 请求参数类
        /// </summary>
        public class UploadParameterType
        {
            public UploadParameterType()
            {
                FileNameKey = "image_file";//"fileName";
                Encoding = Encoding.UTF8;
                PostParameters = new Dictionary<string, string>();
            }
            /// <summary>
            /// 上传地址
            /// </summary>
            public string Url { get; set; }
            /// <summary>
            /// 文件名称key
            /// </summary>
            public string FileNameKey { get; set; }
            /// <summary>
            /// 文件名称value
            /// </summary>
            public string FileNameValue { get; set; }
            /// <summary>
            /// 编码格式
            /// </summary>
            public Encoding Encoding { get; set; }
            /// <summary>
            /// 上传文件的流
            /// </summary>
            public Stream UploadStream { get; set; }
            /// <summary>
            /// 上传文件 携带的参数集合
            /// </summary>
            public IDictionary<string, string> PostParameters { get; set; }
        }
 
 
调用:
  string path = Server.MapPath("/images/"); //文件夹路径
            string[] paths = Directory.GetFiles(path); //获取文件夹下全部文件路径
            for (int i = 0; i < paths.Length; i++)
            {
                using (FileStream fileStream = new FileStream(paths[i], FileMode.Open, FileAccess.Read))
                {
                //   using (FileStream fileStream = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images/000000.jpg"), FileMode.Open, FileAccess.Read)) {
                string[] namea = paths[i].Split(‘\\‘);
                IDictionary<string, string> postParameter = new Dictionary<string, string>();
                postParameter.Add("api_key", "MNMUYvr81rvFSRFogoGxuVx0Xkg6PKCN");
                postParameter.Add("api_secret", "ZOzaN-1CdYhy_Qw6dtkBzaW83eNYhiNP");
                //postParameter.Add("image_url", "https://gss0.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D600%2C800/sign=af933d28ccea15ce41bbe80f863016cb/a08b87d6277f9e2faba04ea51a30e924b999f382.jpg");
               // postParameter.Add("image_file", date2);
                postParameter.Add("return_landmark", "1");
                postParameter.Add("return_attributes", "gender,age,smiling,glass,headpose,facequality,blur");
                string result = HttpUploadClient.Execute(new UploadParameterType
                {
                    Url = "https://api.megvii.com/facepp/v3/detect",
                    UploadStream = fileStream,
                    FileNameValue = http://www.mamicode.com/namea[namea.Length - 1],
                    PostParameters = postParameter
                });
               // Response.Write(result);
                
                 System.IO.File.WriteAllText(Server.MapPath("/txt/" + namea[namea.Length - 1].Split(‘.‘)[0]+".txt"), result);  // 一定要绝对路径  
             }
            }

模拟post或get请求(c u r l),(请求里可以包含图片)