zl程序教程

您现在的位置是:首页 >  工具

当前栏目

apache中文乱码_文件名称乱码怎么解决

Apache中文 解决 怎么 乱码 文件名称
2023-06-13 09:13:44 时间

大家好,又见面了,我是你们的朋友全栈君。

RestSharp是一个第三方开源的Http模拟请求辅助类,其底层实现基于System.Net.HttpWebRequest,且不依赖于任何第三方控件。其github地址为:https://github.com/restsharp/RestSharp,start数可以说明该类库的知名度,当然侧面也可以证明它的确是一个比较好用的HTTP请求辅助类。

一般情况下,RestSharp都工作的很好,只是当服务端地址指定了非Utf-8编码方式时,RestSharp会开始出现各种乱码问题(请求内容乱码、响应内容乱码)。

对于请求部分的Encoding问题,通过RestClient.ConfigureHttp以及Http.CalculateContentLength这两个方法的源代码,发现我们可以通过设置RestClient.Encoding来解决请求部分的编码问题,通过RestClient的构造函数,我们也可以发现其初始值为Encoding.UTF8,当然别忘了设置请求相应的ContentType

var client = new RestClient(url);
client.Encoding = Encoding.GetEncoding("gb2312");
client.AddDefaultHeader("Content-Type", "text/html;charset=gb2312");

而对于IRestResponse部分,RestSharp的处理方式就不是那么友好了,通过RestResponseBase.Content以及MiscExtensions.AsString,我们可以看到其赤果果的简单粗暴,就是只认Encoding.UTF8,也就是对于IRestResponse部分,默认情况下,RestSharp根本就没考虑过Encoding问题,IRestResponse.ContentEncodingIRestResponse.ContentType在这里就是摆设!但幸好,IRestResponse.Content是可读可写的,而且我们也可以通过IRestResponse.RawBytes来得到服务器原始的响应内容,所以我们可以通过自定义一个扩展来自己完成这个编码的转换过程。

    /// <summary>
/// <see cref="RestSharp"/>辅助类
/// </summary>
public static class RestSharpHelper
{ 

/// <summary>
/// 根据<see cref="IRestResponse.ContentEncoding"/>或<see cref="IRestResponse.ContentType"/>设置<see cref="IRestResponse.Content"/>
/// </summary>
/// <param name="response">Rest响应实体</param>
public static void SetResponseEncoding(this IRestResponse response)
{ 

var encoding = response.ContentEncoding;
if (string.IsNullOrWhiteSpace(encoding) && !string.IsNullOrWhiteSpace(response.ContentType))
{ 

var tmp = response.ContentType.Split(';').Select(s => s.Split('='));
var arr = tmp.LastOrDefault(t => t.Length == 2 && t[0].Trim().ToLower() == "charset");
if (arr != null)
{ 

encoding = arr[1].Trim();
}
}
if (!string.IsNullOrWhiteSpace(encoding))
{ 

response.SetResponseEncoding(encoding);
}
}
/// <summary>
/// 根据Encoding设置<see cref="IRestResponse.Content"/>
/// </summary>
/// <param name="response">Rest响应实体</param>
/// <param name="encoding">响应内容编码方式</param>
public static void SetResponseEncoding(this IRestResponse response, string encoding)
{ 

if (!string.IsNullOrWhiteSpace(encoding))
{ 

response.SetResponseEncoding(Encoding.GetEncoding(encoding));
}
}
/// <summary>
/// 根据Encoding设置<see cref="IRestResponse.Content"/>
/// </summary>
/// <param name="response">Rest响应实体</param>
/// <param name="encoding">响应内容编码方式</param>
public static void SetResponseEncoding(this IRestResponse response, Encoding encoding)
{ 

if (encoding != null)
{ 

response.ContentEncoding = encoding.WebName;
response.Content = encoding.GetString(response.RawBytes);
}
}
}

接下来就是在执行请求后简单的调用一次

var request = new RestRequest(Method.POST);
//设置反序列化时预先处理乱码问题,如果调用的是Execute<T>方法,那么后面无需再次调用该方法
request.OnBeforeDeserialization = res => RestSharpHelper.SetResponseEncoding(res, "gb2312");
var response = client.Execute(request);
//直接读取Content还是需要调用该方法
RestSharpHelper.SetResponseEncoding(response, "gb2312");

注意在这里我们对IRestRequest.OnBeforeDeserialization进行了赋值,该委托主要用于反序列化时解决Encoding导致的乱码问题,所以说其实RestSharp是知道有这个问题,但没有对该问题进行处理……

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/196099.html原文链接:https://javaforall.cn