C#验证码识别接口调用代码实例

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

/**
*验证码识别调用示例代码 - 聚合数据
*在线接口文档:http://www.juhe.cn/docs/60
**/

namespace Ver_code
{
    class Program
    {
        /// <summary>
        /// 验证码识别接口调用代码实例
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            NameValueCollection nvc = new NameValueCollection();
            //申请的key
            nvc.Add("key", "");
            //验证码类型代码
            nvc.Add("codeType", "");
            //验证码接口地址
            string ApiUrl = "http://op.juhe.cn/vercode/index";
            //验证码图片路径
            string ver_img = @"G:\img\verifyhandler.jpg";
            string result = HttpUploadFile(ApiUrl, ver_img, "image", "image/jpeg", nvc);
            Debug.WriteLine(result);
        }

        /// <summary>
        /// POST图片等文件(带参数)
        /// </summary>
        /// <param name="url">接口地址</param>
        /// <param name="file">上传文件路径</param>
        /// <param name="paramName">参数名称</param>
        /// <param name="contentType">类型</param>
        /// <param name="nvc">参数</param>
        /// <returns></returns>
        public static string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
        {
            string result = string.Empty;
            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 header = string.Format(headerTemplate, paramName, file, contentType);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(file, 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;
        }
    }
}


登录 后可发表评论

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