.net core HttpClient Post FormData 文件 两种方式

一 、第一种使用文件流 StreamContent

public async Task<string> PostFileStream(string apiUrl, Stream stream, string fileName)
{
    var content = new MultipartFormDataContent
    {
     { new StreamContent(stream,(int)stream.Length), "file", fileName }
    };

    System.Net.Http.HttpClient _client = new System.Net.Http.HttpClient();

    var response = await _client.PostAsync(apiUrl, content);
    response.EnsureSuccessStatusCode();

    var responseContent = response.Content.ReadAsStringAsync().Result;
    return responseContent;
}

二、第二种使用byte数组 ByteArrayContent

public async Task<string> PostFileByteArray(string apiUrl, byte[] bytes, string fileName)
{
    var content = new MultipartFormDataContent
    {
     { new ByteArrayContent(bytes), "file", fileName }
    };

    System.Net.Http.HttpClient _client = new System.Net.Http.HttpClient();

    var response = await _client.PostAsync(apiUrl, content);
    response.EnsureSuccessStatusCode();

    var responseContent = response.Content.ReadAsStringAsync().Result;
    return responseContent;
}

若第一种API无法接受到文件可以尝试第二种方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将XML请求转换为application/x-www-form-urlencoded模型,您需要对XML请求进行解析,并将其转换为.NET对象。然后,您可以使用.NET的内置方法将.NET对象转换为application/x-www-form-urlencoded格式的字符串。 以下是一个示例,演示如何使用XmlSerializer将XML请求转换为.NET对象,并使用System.Net.Http.Formatting库将.NET对象转换为application/x-www-form-urlencoded格式的字符串: ```csharp using System.Net.Http.Formatting; using System.Xml.Serialization; // 定义XML请求实体类 [XmlRoot("OTA_InventoryCheckRQ")] public class InventoryCheckRequest { [XmlAttribute("EchoToken")] public string EchoToken { get; set; } [XmlAttribute("Password")] public string Password { get; set; } [XmlAttribute("PrimaryLangID")] public string PrimaryLangID { get; set; } [XmlAttribute("TimeStamp")] public string TimeStamp { get; set; } [XmlAttribute("UserName")] public string UserName { get; set; } [XmlAttribute("Version")] public string Version { get; set; } [XmlElement("HotelReservations")] public HotelReservations HotelReservations { get; set; } } public class HotelReservations { [XmlElement("HotelReservation")] public HotelReservation HotelReservation { get; set; } } public class HotelReservation { [XmlElement("RoomStay")] public RoomStay RoomStay { get; set; } [XmlElement("ResGlobalInfo")] public ResGlobalInfo ResGlobalInfo { get; set; } } public class RoomStay { [XmlElement("RoomTypes")] public RoomTypes RoomTypes { get; set; } [XmlElement("RatePlans")] public RatePlans RatePlans { get; set; } [XmlElement("GuestCounts")] public GuestCounts GuestCounts { get; set; } [XmlElement("BasicPropertyInfo")] public BasicPropertyInfo BasicPropertyInfo { get; set; } } public class RoomTypes { [XmlElement("RoomType")] public RoomType RoomType { get; set; } } public class RoomType { [XmlAttribute("RoomTypeCode")] public string RoomTypeCode { get; set; } } public class RatePlans { [XmlElement("RatePlan")] public RatePlan RatePlan { get; set; } } public class RatePlan { [XmlAttribute("RatePlanCode")] public string RatePlanCode { get; set; } } public class GuestCounts { [XmlElement("GuestCount")] public List<GuestCount> GuestCount { get; set; } } public class GuestCount { [XmlAttribute("AgeQualifyingCode")] public string AgeQualifyingCode { get; set; } [XmlAttribute("Count")] public int Count { get; set; } } public class BasicPropertyInfo { [XmlAttribute("HotelCode")] public string HotelCode { get; set; } } public class ResGlobalInfo { [XmlElement("RoomCount")] public int RoomCount { get; set; } [XmlElement("MemberLevel")] public string MemberLevel { get; set; } [XmlElement("TimeSpan")] public TimeSpan TimeSpan { get; set; } } public class TimeSpan { [XmlAttribute("End")] public string End { get; set; } [XmlAttribute("Start")] public string Start { get; set; } } // 在控制器中处理请求 public async Task<IActionResult> ProcessRequest([FromBody] string xmlRequest) { // 解析XML请求 XmlSerializer serializer = new XmlSerializer(typeof(InventoryCheckRequest)); InventoryCheckRequest request; using (StringReader reader = new StringReader(xmlRequest)) { request = (InventoryCheckRequest)serializer.Deserialize(reader); } // 将.NET对象转换为application/x-www-form-urlencoded格式的字符串 var formData = new FormUrlEncodedContent(new Dictionary<string, string> { { "EchoToken", request.EchoToken }, { "Password", request.Password }, { "PrimaryLangID", request.PrimaryLangID }, { "TimeStamp", request.TimeStamp }, { "UserName", request.UserName }, { "Version", request.Version }, { "HotelReservations.HotelReservation.RoomStay.RoomTypes.RoomType.RoomTypeCode", request.HotelReservations.HotelReservation.RoomStay.RoomTypes.RoomType.RoomTypeCode }, { "HotelReservations.HotelReservation.RoomStay.RatePlans.RatePlan.RatePlanCode", request.HotelReservations.HotelReservation.RoomStay.RatePlans.RatePlan.RatePlanCode }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].AgeQualifyingCode", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].AgeQualifyingCode }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].Count", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].Count.ToString() }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].AgeQualifyingCode", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].AgeQualifyingCode }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].Count", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].Count.ToString() }, { "HotelReservations.HotelReservation.RoomStay.BasicPropertyInfo.HotelCode", request.HotelReservations.HotelReservation.RoomStay.BasicPropertyInfo.HotelCode }, { "HotelReservations.HotelReservation.ResGlobalInfo.RoomCount", request.HotelReservations.HotelReservation.ResGlobalInfo.RoomCount.ToString() }, { "HotelReservations.HotelReservation.ResGlobalInfo.MemberLevel", request.HotelReservations.HotelReservation.ResGlobalInfo.MemberLevel }, { "HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.Start", request.HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.Start }, { "HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.End", request.HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.End } }); // 发送POST请求 HttpClient client = new HttpClient(); HttpResponseMessage response = await client.PostAsync("https://example.com", formData); // 处理响应并返回结果 string responseBody = await response.Content.ReadAsStringAsync(); return Ok(responseBody); } ``` 请注意,上述代码仅供参考。您需要根据自己的实际情况进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值