|
基于人工智能的通用图片识别平台
开发文档
C#

脚本

Generated code for C# - HttpClient

HttpClient 用法不了解的请百度 HttpClient教程
        

           using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Collections.Generic;

namespace HttpClientExample
{
    public class PredictRequest
    {
        // 必填参数
        public string username { get; set; }
        public string password { get; set; }

        public string typeid { get; set; }
        public byte[] image { get; set; }

        // 可选参数
        /// 
        /// 旋转角度设置,用于旋转类验证码
        /// 
        public string angle { get; set; }
        /// 
        /// 每次旋转的角度,用于旋转类验证码
        /// 
        public string step { get; set; }
        /// 
        /// 自定义验证码类型名称
        /// 
        public string typename { get; set; }
        /// 
        /// 备注信息
        /// 
        public string remark { get; set; }
        /// 
        /// 背景图片,用于背景匹配类型验证码
        /// 
        public byte[] imageback { get; set; }
        /// 
        /// 标题内容
        /// 
        public string content { get; set; }
        /// 
        /// 标题图片
        /// 
        public byte[] title_image { get; set; }

        public void ValidateRequiredFields()
        {
            if (string.IsNullOrEmpty(username))
                throw new ArgumentException("username是必填参数");
            if (string.IsNullOrEmpty(password))
                throw new ArgumentException("password是必填参数");
            if (string.IsNullOrEmpty(typeid))
                throw new ArgumentException("typeid是必填参数");
            if (image == null)
                throw new ArgumentException("image是必填参数");
        }
    }

    public class ApiClient
    {
        private readonly HttpClient _httpClient;
        private readonly string _baseUrl = "http://api.ttshitu.com/predict";

        public ApiClient()
        {
            _httpClient = new HttpClient();
        }

        // 使用 multipart/form-data 格式发送请求
        public async Task PredictWithFormDataAsync(PredictRequest request)
        {
            request.ValidateRequiredFields();
            using (var formData = new MultipartFormDataContent())
            {
                if (!string.IsNullOrEmpty(request.username))
                    formData.Add(new StringContent(request.username), "username");
                if (!string.IsNullOrEmpty(request.password))
                    formData.Add(new StringContent(request.password), "password");
                if (!string.IsNullOrEmpty(request.typeid))
                    formData.Add(new StringContent(request.typeid), "typeid");
                if (!string.IsNullOrEmpty(request.angle))
                    formData.Add(new StringContent(request.angle), "angle");
                if (!string.IsNullOrEmpty(request.step))
                    formData.Add(new StringContent(request.step), "step");
                if (!string.IsNullOrEmpty(request.typename))
                    formData.Add(new StringContent(request.typename), "typename");
                if (!string.IsNullOrEmpty(request.remark))
                    formData.Add(new StringContent(request.remark), "remark");

                if (request.image != null)
                    formData.Add(new ByteArrayContent(request.image), "image", "image.jpg");
                if (request.imageback != null)
                    formData.Add(new ByteArrayContent(request.imageback), "imageback", "imageback.jpg");
                if (!string.IsNullOrEmpty(request.content))
                    formData.Add(new StringContent(request.content), "content");
                if (request.title_image != null)
                    formData.Add(new ByteArrayContent(request.title_image), "title_image", "title_image.jpg");

                var response = await _httpClient.PostAsync(_baseUrl, formData);
                return await response.Content.ReadAsStringAsync();
            }
        }

        // 使用 application/json 格式发送请求
        public async Task PredictWithJsonAsync(PredictRequest request)
        {
            request.ValidateRequiredFields();
            var jsonRequest = new Dictionary();

            if (!string.IsNullOrEmpty(request.username))
                jsonRequest["username"] = request.username;
            if (!string.IsNullOrEmpty(request.password))
                jsonRequest["password"] = request.password;
            if (!string.IsNullOrEmpty(request.typeid))
                jsonRequest["typeid"] = request.typeid;
            if (!string.IsNullOrEmpty(request.angle))
                jsonRequest["angle"] = request.angle;
            if (!string.IsNullOrEmpty(request.step))
                jsonRequest["step"] = request.step;
            if (!string.IsNullOrEmpty(request.typename))
                jsonRequest["typename"] = request.typename;
            if (!string.IsNullOrEmpty(request.remark))
                jsonRequest["remark"] = request.remark;

            if (request.image != null)
                jsonRequest["image"] = Convert.ToBase64String(request.image);
            if (request.imageback != null)
                jsonRequest["imageback"] = Convert.ToBase64String(request.imageback);
            if (!string.IsNullOrEmpty(request.content))
                jsonRequest["content"] = request.content;
            if (request.title_image != null)
                jsonRequest["title_image"] = Convert.ToBase64String(request.title_image);

            var jsonContent = JsonSerializer.Serialize(jsonRequest);
            var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(_baseUrl, stringContent);
            return await response.Content.ReadAsStringAsync();
        }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                var client = new ApiClient();
                var request = new PredictRequest
                {
                    username = "your_username",
                    password = "your_password",
                     /// 
                    /// 验证码类型ID,可选值:
                    /// 1:纯数字
                    /// 1001:纯数字2
                    /// 2:纯英文
                    /// 1002:纯英文2
                    /// 3:数英混合
                    /// 1003:数英混合2
                    /// 4:闪动GIF
                    /// 7:无感学习(独家)
                    /// 66:问答题
                    /// 11:计算题
                    /// 1005:快速计算题
                    /// 5:快速计算题2
                    /// 16:汉字
                    /// 32:通用文字识别(证件、单据)
                    /// 29:旋转类型
                    /// 1029:背景匹配旋转类型(需要2张图 一张中间的小图一张背景图)
                    /// 2029:背景匹配双旋转类型(需要2张图 一张中间的小图一张背景图)
                    /// 19:点选1个坐标
                    /// 20:点选3个坐标
                    /// 21:点选3~5个坐标
                    /// 22:点选5~8个坐标
                    /// 27:点选1~4个坐标
                    /// 48:轨迹类型
                    /// 18:缺口识别(需要2张图一张目标图一张缺口图)
                    /// 33:单缺口识别(返回X轴坐标 只需要1张图)
                    /// 34:缺口识别2(返回X轴坐标 只需要1张图)
                    /// 53:拼图识别
                    /// 
                    typeid = "1",
                    image = File.ReadAllBytes("test.jpg") // 确保test.jpg文件存在
                };

                // 使用FormData方式调用
                Console.WriteLine("使用FormData方式调用API:");
                var formDataResult = await client.PredictWithFormDataAsync(request);
                Console.WriteLine($"FormData响应: {formDataResult}\n");

                // 使用JSON方式调用
                Console.WriteLine("使用JSON方式调用API:");
                var jsonResult = await client.PredictWithJsonAsync(request);
                Console.WriteLine($"JSON响应: {jsonResult}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
        }
    }
}

        
    
 
您好,有什么需要帮助的吗?