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
| using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using ReqMvc.Models;
using Microsoft.Net.Http.Headers;
namespace ReqMvc.Controllers;
public class SayController : Controller
{
private readonly IHttpClientFactory _httpClientFactory;
public SayController(IHttpClientFactory httpClientFactory) => _httpClientFactory=httpClientFactory;
public string sayHi()
{
var requestMessage=new HttpRequestMessage(
HttpMethod.Get,
"https://localhost:7006/HelloWorld/Hi")
{
Headers=
{
{HeaderNames.Accept, "text/plain"},
{HeaderNames.UserAgent, "HttpRequestsSample"}
}
};
var httpClient=_httpClientFactory.CreateClient();
var res=httpClient.Send(requestMessage);
byte[] msg=new byte[100];
//return res.ToString();
res.Content.ReadAsStream().Read(msg);
string strmsg=System.Text.Encoding.UTF8.GetString(msg).TrimEnd('\0');
Console.WriteLine(strmsg);
return strmsg;
}
public string ret(){
string str="just string";
return str;
}
}
|
Leave a comment