违章代缴图片上传C#版本

/**
*证件识别接口调用示例 - 聚合数据
*在线接口文档:http://www.juhe.cn/docs/api/id/238
**/

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace Test_Demo
{
    class 违章代缴证件上传
    {
        /// <summary>
        /// 证件识别接口调用实例
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            NameValueCollection nvc = new NameValueCollection();
            //申请的key
            nvc.Add("key", "申请的key");
            //订单号
            nvc.Add("userOrderId", "订单号");
            //证件上传接口地址
            string ApiUrl = "http://v.juhe.cn/wzdj/upload.php";
            //证件图片路径
            string ImgUrl = @"C:\uptest.jpg";
            string result = HttpUploadFile(ApiUrl, ImgUrl, "A", nvc);
            //识别结果
            Debug.WriteLine(result);
        }

        /// <summary>
        /// POST图片等文件(带参数)
        /// </summary>
        /// <param name="url">接口地址</param>
        /// <param name="filePath">上传文件路径</param>
        /// <param name="paramName">参数名称</param>
        /// <param name="contentType">类型</param>
        /// <param name="nvc">参数</param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string filePath, string paramName, NameValueCollection nvc)
        {
            string result = string.Empty;
            string header = "";
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;
            wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Stream rs = wr.GetRequestStream();

            string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
            foreach (string key in nvc.Keys)
            {
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                string formitem = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                rs.Write(formitembytes, 0, formitembytes.Length);
            }
            rs.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

            string extension = System.IO.Path.GetExtension(filePath);
            if (extension == ".jpg")
            {
                header = string.Format(headerTemplate, paramName, filePath, "image/jpeg");
            }
            else
            {
                header = string.Format(headerTemplate, paramName, filePath, "application/octet-stream");
            }
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();

            WebResponse wresp = null;
            try
            {
                wresp = wr.GetResponse();
                Stream stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);

                result = reader2.ReadToEnd();
            }
            catch (Exception ex)
            {
                if (wresp != null)
                {
                    wresp.Close();
                    wresp = null;
                }
            }
            finally
            {
                wr = null;
            }

            return result;
            Debug.WriteLine(result);
        }
    }
}


登录 后可发表评论

    最新发布
    聚合数据©版权所有