-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpUtils.cs
107 lines (99 loc) · 3.95 KB
/
HttpUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class HttpUtils
{
static HttpClient client = new HttpClient();
public static async Task<HttpResponse> PostSseAsync(string url, Dictionary<string, string> headers, string body, Action<string> consumer)
{
using (var request = new HttpRequestMessage(HttpMethod.Post, url))
{
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
var contentType = response.Content.Headers.GetValues("Content-Type").FirstOrDefault("");
if (response.IsSuccessStatusCode && contentType.Contains("text/event-stream"))
{
var stream = await response.Content.ReadAsStreamAsync();
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync();
if (line != null && line.StartsWith("data:"))
{
var data = line.Substring(line.StartsWith("data: ") ? 6 : 5);
if (string.IsNullOrEmpty(data) || data == "[DONE]")
{
continue;
}
consumer(data);
}
}
}
return new HttpResponse((int)response.StatusCode, contentType, null);
}
else
{
var text = await response.Content.ReadAsStringAsync();
return new HttpResponse((int)response.StatusCode, contentType, text);
}
}
}
}
public static async Task<HttpResponse> PostJsonAsync(string url, Dictionary<string, string> headers, string body)
{
using (var request = new HttpRequestMessage(HttpMethod.Post, url))
{
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
using (var response = await client.SendAsync(request))
{
var text = await response.Content.ReadAsStringAsync();
var contentType = response.Content.Headers.GetValues("Content-Type").FirstOrDefault("");
return new HttpResponse((int)response.StatusCode, contentType, text);
}
}
}
public static async Task DownloadAsync(string url, string savePath)
{
using (var response = await client.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
var fileBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync(savePath, fileBytes);
}
}
}
public class HttpResponse
{
int statusCode;
string contentType;
string? text;
public HttpResponse(int statusCode, string contentType, string? text)
{
this.statusCode = statusCode;
this.contentType = contentType;
this.text = text;
}
public int StatusCode { get { return statusCode; } }
public string ContentType { get { return contentType; } }
public string? Text { get { return text; } }
}
}