我有一个C#客户端,它调用REST API来记录我的数据。
我的客户端代码非常简单:
public class Tester
{
private readonly string _baseUri = "URI";
private readonly string _createUpdateLogUri = "v1.0/log";
private readonly HttpClient _httpClient;
private readonly MediaTypeHeaderValue _textPlainContent = MediaTypeHeaderValue.Parse("text/plain");
private readonly Encoding _encoding = Encoding.GetEncoding("ISO-8859-1");
public Tester()
{
_httpClient = new HttpClient
{
BaseAddress = new Uri(_baseUri),
Timeout = new TimeSpan(0, 5, 0)
};
}
public void Log(Head head, string ticket, string clientIp, string clientName, string headIp, string headName)
{
var main = new Main
{
Ticket = Convert.ToBase64String(_encoding.GetBytes(ticket)),
Application = head.ApplicationCaller,
ClientApplication = head.ApplicationCode,
ClientIp = clientIp,
ClientName = clientName,
ClientUser = head.User,
Command = head.Command,
HeaderIp = headIp,
HeaderName = headName,
HeaderTimestamp = DateTime.UtcNow,
Module = head.Module,
Guid = head.CallIdentifier,
CreatedTimestamp = DateTime.UtcNow,
};
var mainToStringContent = ObjectToStringContent(main);
var headToXml = XmlHelper<Head>.SerializeObjectToXml(head);
var headToBytes = _encoding.GetBytes(headToXml);
var headContent = new ByteArrayContent(headToBytes, 0, headToBytes.Length);
headContent.Headers.ContentType = _textPlainContent;
var form = new MultipartFormDataContent
{
{
headContent, "header", "head.txt"
},
{
mainToStringContent, "main"
}
};
try
{
var result = _httpClient.PostAsync(_createUpdateLogUri, form).Result;
}
catch (SocketException e)
{
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private StringContent ObjectToStringContent<T>(T myObject)
{
var jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
var objectToJson = JsonConvert.SerializeObject(myObject, Formatting.Indented, jsonSettings);
return new StringContent(objectToJson, _encoding);
}
}
要检查呼叫是否成功,我使用fiddler捕获流量,当客户端执行更多呼叫时,在某些情况下(例如4/100),我收到此错误。
[Fiddler]与&#39; URI&#39;的连接失败。
错误: ConnectionRefused(0x274d)。
System.Net.Sockets.SocketException 由于目标机器是主动的,因此无法建立连接 拒绝它URI:80
但是在我的客户端,这个异常从未被捕获过, 然后验证日志,不记录一些数据 我怎么解决这个问题??我的代码或流量在我的服务器上? 提前感谢您的回答。