winform 使用post 请求(JSON/图片[图片放大])和get 请求

261 阅读1分钟

一般情况下可以用postman先调用测试,测试完后postman右侧自动生成的代码可以直接使用

回复接收对象(可用可不用)

internal class Result
{
    public string statusCode { get; set; }
    public string message { get; set; }
    public string data { get; set; }
    public string code { get; set; }
}

使用C#进行GET请求:

        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            try
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
            finally
            {
                stream.Close();
            }
        }
        catch (Exception ex) { }
        return "";

使用C#进行POST请求,方法一(普通JSON):

    try{
            Dictionary<string, string> dic = new Dictionary<string, string>();
            // 替换写法
            //    dic.ForEach(dic =>
            //      {
            //          request.AddParameter(dic.Key, dic.Value);
            //     });
            dic.Add("userName", phone);
            dic.Add("password", pwd);
            dic.Add("checkCode", code);
            dic.Add("checkId", uuid);
            dic.Add("loginType", "1");
            string url = "https://..................";
            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            dic.ForEach(dic =>
            {
                request.AddParameter(dic.Key, dic.Value);
            });
            IRestResponse response = client.Execute(request);
            if (response != null)
            {
                return JsonConvert.DeserializeObject<LoginData>(response.Content);
            }
        }
        catch (Exception ex)
        {
            return null;
        }
        

使用C#进行POST请求,方法二(普通JSON):

    var client = new RestClient("https://......=");
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddHeader("Cookie", "HttpOnly=true");
    request.AddParameter("name", nameList[i]);
    request.AddParameter("code", idList[i]);
    IRestResponse response = client.Execute(request);
    // 对象需要新建
    Result r = JsonConvert.DeserializeObject<Result>(response.Content);
    if (r != null && r.message == "校验成功")
    ...
    

使用C#进行POST请求(图片信息):

public static string PostImg(string url)
{
    var client = new RestClient(url);
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Cookie", "HttpOnly=true; AISESSIONID=cfbe82f0-5ad6-4011-984d-db94ef5bac56");
    IRestResponse response = client.Execute(request);
    return Convert.ToBase64String(response.RawBytes);
}

图片放大功能

    private static Bitmap getAfterApiImg(string base64String)
    {
        // 原图片
        Image img = Image.FromStream(new MemoryStream(Convert.FromBase64String(base64String)));
        Bitmap b = new Bitmap(200, 75);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //绘制新图像
        g.DrawImage(img, 0, 0, 200, 75);
        g.Dispose();
        return b;
    }