我正在尝试使用WebClient调用发布包含Unicode nDash字符(–
)的字符串:
{
"data": "This is a test – string"
}
当我尝试发布时,WebClient收到空值。
这是我的WebClient呼叫:
using (IDisposable context = System.Web.Hosting.HostingEnvironment.Impersonate()) {
var data = false;
using (var client = new WebClientCall()) {
client.BaseAddress = "https://BaseAddress"
client.Headers.Clear();
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Credentials = CredentialCache.DefaultCredentials;
var dataString = JsonConvert.SerializeObject(messageContents);
var json = client.UploadString($"api/data", "POST", dataString);
data = JsonConvert.DeserializeObject<bool>(json, new JsonSerializerSettings() {
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
return data;
}
}
private class WebClientCall : WebClient {
protected override WebRequest GetWebRequest(Uri address) {
HttpWebRequest webRequest = base.GetWebRequest(address) as HttpWebRequest;
if (webRequest != null) {
webRequest.Timeout = (int)TimeSpan.FromMinutes(10).TotalMilliseconds;
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
return (WebRequest)webRequest;
}
}
有人可以帮我在WebClient呼叫中发送非ASCII字符吗?