55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
|
|
namespace UdpClientLib
|
|
{
|
|
public abstract class NetworkClientBase
|
|
{
|
|
public string Name { get; set; }
|
|
public IPEndPoint _serverEndPoint;
|
|
|
|
// 공통 HTTP 요청 메서드
|
|
public string CreateHttpRequest(HttpRequest request)
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($"{request.Method} {request.Path} HTTP/1.1");
|
|
sb.AppendLine($"Host: {_serverEndPoint?.Address}:{_serverEndPoint?.Port}");
|
|
|
|
var headers = request.Headers ?? new Dictionary<string, string>();
|
|
|
|
if (!headers.ContainsKey("Content-Type"))
|
|
{
|
|
headers["Content-Type"] = "application/json;charset=UTF-8";
|
|
}
|
|
|
|
if (!headers.ContainsKey("User-Agent"))
|
|
{
|
|
headers["User-Agent"] = $"UdpClient-{Name}/2.0";
|
|
}
|
|
|
|
foreach (var (key, value) in headers)
|
|
{
|
|
sb.AppendLine($"{key}: {value}");
|
|
}
|
|
|
|
if (request.Method is HttpMethod.POST or HttpMethod.PUT or HttpMethod.PATCH)
|
|
{
|
|
int contentLength = request.Body?.Length ?? 0;
|
|
sb.AppendLine($"Content-Length: {contentLength}");
|
|
}
|
|
|
|
sb.AppendLine();
|
|
|
|
if (!string.IsNullOrEmpty(request.Body))
|
|
{
|
|
sb.Append(request.Body);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|
|
|