.net 5.0改成6.0 修复外网文件上传bug

This commit is contained in:
范露尧
2023-03-07 23:32:57 +08:00
parent 923622a4bb
commit d0450c9585
5 changed files with 59 additions and 12 deletions

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Getf_Service_Transfer_Client_Service_HttpHandler
{
public class BodyEntity
{
/// <summary>
///
/// </summary>
public string commond { get; set; }
/// <summary>
///
/// </summary>
public byte[] bytes { get; set; }
/// <summary>
///
/// </summary>
public string FileName { get; set; }
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>

View File

@@ -16,6 +16,7 @@ using System.Reflection;
using System.Net.Http;
using Furion.RemoteRequest.Extensions;
using Furion.RemoteRequest;
using System.Security.Policy;
namespace Getf_Service_Transfer_Client_Service_HttpHandler
{
@@ -51,13 +52,31 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
}
object r;
SetHead(httpRequst, jObject["Head"]);
httpRequst.SetBody(jObject["Data"].ToString());
if (data.Length > 0)
var bodyData = jObject["Data"];
var bodyParam = jObject["Param"];
if (data != null && data.Length > 0)
{
httpRequst.SetRequestUrl(url + "byte");
httpRequst.SetBody(new { FileName = jObject["Data"]["fileName"].ToString(), bytes = data });
BodyEntity be = new();
if (bodyData != null)
{
if (bodyData["commond"] != null)
be.commond = bodyData["commond"].ToString();
if (bodyData["fileName"] != null)
be.FileName = bodyData["fileName"].ToString();
be.bytes = data;
}
var response = httpRequst.SendAsync().Result;
httpRequst.SetBody(be);
_LogHelper.Info($"body:{Newtonsoft.Json.JsonConvert.SerializeObject(be)}");
}
else
{
if (bodyParam != null)
httpRequst.SetBody(bodyParam.ToString());
if (bodyData != null)
httpRequst.SetBody(bodyData.ToString());
}
var response = httpRequst.SendAsync()?.Result;
if (IsTextContentType(response.Content.Headers.ContentType.MediaType))
{
r = response.Content.ReadAsStringAsync().Result;
@@ -102,7 +121,8 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
catch (Exception e)
{
msg = e.Message;
_LogHelper.Info("3:请求错误原因为:" + msg);
_LogHelper.Info($"3:请求错误原因为:93段接口:url:{url},参数:{jObject["Data"]},结果:{msg},{e.StackTrace}");
//_LogHelper.Info("3:请求错误原因为:" + msg + "," + e.StackTrace);
}
return null;
}
@@ -200,12 +220,14 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
{
foreach (JProperty elem in head)
{
var name = elem.Name;
var value = elem.Value.ToString();
var name = elem?.Name;
var value = elem?.Value?.ToString();
if (!string.IsNullOrEmpty(name))
dic.Add(name, value);
}
}
httprequest.SetHeaders(dic);
_LogHelper.Info($"6:请求header:{dic}");
}
private void SetHead(HttpWebRequest httpWebRequest, string method, string headStr)
@@ -282,7 +304,8 @@ namespace Getf_Service_Transfer_Client_Service_HttpHandler
{
if (contentType == null) return true;
contentType = contentType.ToLower();
var list = new string[] { "text/", "application/json", "application/xml" };
//"text/",
var list = new string[] { "application/json", "application/xml" };
return list.Any(m => contentType.Contains(m));
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>dotnet-Getf_Service_Transfer_core_client-A49AB986-D7BC-4882-BAB1-48CAF75741A2</UserSecretsId>
</PropertyGroup>