• c# post方式请求java form表单api


    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    
    namespace ConsoleApplication1
    {
        public class HttpUtil
        {
            public static string test()
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        using (var multipartFormDataContent = new MultipartFormDataContent())
                        {
                            var values = new[]
                            {
                                new KeyValuePair<string, string>("to", "tomfang@126.com"),
                                new KeyValuePair<string, string>("from", "tomfang@126.com"),
                                new KeyValuePair<string, string>("subject", "test"),
                                new KeyValuePair<string, string>("bodyText", "test"),
                                new KeyValuePair<string, string>("cc", "tomfang@126.com")
                            };
                            foreach (var keyValuePair in values)
                            {
                                multipartFormDataContent.Add(new StringContent(keyValuePair.Value), String.Format(""{0}"", keyValuePair.Key));
                            }
    
                            multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:1.txt")), "attchfile", ""test.jpg"");
                            multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:2.txt")), "attchfile", ""test.jpg"");
                            multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:3.txt")), "attchfile", ""test.jpg"");
                            var requestUri = "http://localhost:8081/house/sendEmail";
                            var html = client.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
                            return html;
                        }
                    }
                }
                catch (Exception exx)
                {
                    throw exx;
                }
            }
        }
    }
  • 相关阅读:
    sublime的ctags安装
    微信授权
    [读码时间] 图片列表:鼠标移入/移出改变图片透明度
    [读码时间] 提示框效果
    [读码时间] 鼠标移入移出改变样式
    [读码时间] 数组求和
    [读码时间] 简易选项卡
    [读码时间] 简易JS年历
    [读码时间] 记住密码提示框
    [读码时间] 函数传参,改变Div任意属性的值
  • 原文地址:https://www.cnblogs.com/tomfang/p/11055691.html
Copyright © 2020-2023  润新知