1) HTTP 서버
① System.Net Namespace 사용

using System.Net


② HTTP 서버 구동(ex. “http://127.0.0.1:8080/")

HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://127.0.0.1:8080/");
listener.Start();


③ HTTP 요청 처리 (ex. ‘HelloWorld’ 응답

var context = listener.GetContext();
Console.WriteLine("Request : " + context.Request.Url);
byte[] data = Encoding.UTF8.GetBytes("HelloWorld");
context.Response.OutputStream.Write(data, 0, data.Length);
context.Response.StatusCode = 200;
context.Response.Close();


2) HTTP 클라이언트

System.Net.Http Namespace 사용

using System.Net.Http


② HTTP 요청 응답 (ex. “http://127.0.0.1: helloworld")

HttpClient client = new HttpClient();
var res = client.GetAsync("http://127.0.0.1:8080/helloworld").Result;
Console.WriteLine("Response : " + res.StatusCode);



Json  변환 

① Newtonsoft.Json Namespace 사용

using Newtonsoft.Json
using Newtonsoft.Json.Linq


② Json 변환 (ex. string <--> JObject)

JObject json = new JObject();
json
["name"] = "John Doe";
json["salary"] = 300100;
string jsonstr = json.ToString();
Console.WriteLine("Json : " + jsonstr);
JObject json2 = JObject.Parse(jsonstr);
Console.WriteLine($"Name : {json2["name"]}, Salary : {json2["salary"]}");



HTTP 추가 관련 코드

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace HTTP_SERVER
{
    class MyServer
    {
        static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://127.0.0.1:8080/");
            listener.Start();

            while(true)
            {
                var context = listener.GetContext();
                Console.WriteLine(string.Format("Request({0}) : {1}", context.Request.HttpMethod, context.Request.Url));
                DateTime dt = DateTime.Now;
                String strRes = "";
                if (context.Request.HttpMethod == "GET")
                {
                    strRes = dt.ToString();
                }
                else if (context.Request.HttpMethod == "POST")
                {
                    strRes = "(POST) " + dt.ToString();
                    Systehttp://m.IO.Stream body = context.Request.InputStream;
                    System.Text.Encoding encoding = context.Request.ContentEncoding;
                    System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
                    string s = reader.ReadToEnd();
                    System.IO.Directory.CreateDirectory(".\\Output");
                    string fileName = string.Format(".\\Output\\{0:D2}{1:D2}{2:D2}.json", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                    StreamWriter sw = new StreamWriter(fileName);
                    sw.WriteLine(s);
                    sw.Close();
                    body.Close();
                    reader.Close();
                }
                else
                {

                }
                
                byte[] data = Encoding.UTF8.GetBytes(strRes);
                context.Response.OutputStream.Write(data, 0, data.Length);
                context.Response.StatusCode = 200;
                context.Response.Close();
            }
        }
    }
}

'프로그래밍 > C#' 카테고리의 다른 글

서버 클라이언트 예제 1  (0) 2024.05.14
HTTP client  (0) 2024.05.13
Json 2  (0) 2024.05.13
Json  (0) 2024.05.13
프로세스와 쓰레드 샘플  (0) 2024.05.13

+ Recent posts