C# Sending HTTP Requests And Receiving HTTP Responses - HTTP 요청 보내기
C# 2022. 1. 23. 23:00 |반응형
HTTP 요청을 보내고 응답을 받아 보자.
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace ConsoleApp1
{
class Program
{
// HttpClient is intended to be instantiated once per application, rather than per-use.
static readonly HttpClient httpClient = new HttpClient();
static async Task Main(string[] args)
{
try
{
Console.Write("Enter URI: ");
string uriString = Console.ReadLine();
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(uriString.Trim());
httpResponseMessage.EnsureSuccessStatusCode();
// Throws an exception if the IsSuccessStatusCode property for the HTTP response is false.
string responseBody = await httpResponseMessage.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
//string responseBody = await httpClient.GetStringAsync(uriString.Trim());
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine(e.Message);
}
}
}
}
|
소스를 입력하고 빌드한다.
반응형
'C#' 카테고리의 다른 글
C# AdsJumbo - 윈도우 앱(프로그램)에 광고 넣기 (0) | 2022.02.03 |
---|---|
C# JSON - JSON 데이터 파싱하고 원하는대로 보여주기 (3) | 2022.01.26 |
C# Request Data By Using The WebRequest Class - 웹 데이터 요청하기 (0) | 2022.01.23 |
C# with Microsoft Excel - 엑셀 데이터 다루기 (0) | 2022.01.16 |
OpenCvSharp Simple Camera Example (0) | 2022.01.14 |