GET, POST?
클라이언트가 웹 서버로 무언가를 요청하기 위해 데이터를 보낼 때 이 데이터를 우리는 HTTP패킷이라 한다. 이러한 HTTP 패킷의 구조는 크게 헤더와 바디로 구성된다. GET과 POST는 이러한 HTTP패킷에서 자료를 보내는 방식이라 생각하면 될 것 같다. 즉, GET은 보내고자 하는 정보를 URL에 붙여서 보내는 방식이고, POST는 보내고자 하는 정보를 바디에 붙여서 보내는 방식이다. 두 방식은 보안적 측면에서 URL이 노출되므로 GET보다 POST 안전하다고 하고, GET 메소드는 캐싱을 사용하므로POST 방식보다 속도적 측면에서 빠르다고 한다.
python
import requests
import json
url = "<Host URL>"
data= {"KEY1":"VALUES","KEY2":"VALUES"}
r = requests.post(url, data=data)
print(r.status_code)
print(r.text)
C#
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
public class SimpleReq
{
public string? id, password, client_type;
}
class Program
{
private static readonly HttpClient client = new HttpClient();
private static readonly string host = "<Host URL>";
public static string ToJsonString<T>(T data)
{
var stream1 = new MemoryStream();
var ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(stream1, data);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
var jsonBody = sr.ReadToEnd();
return jsonBody;
}
static async Task<bool> RequestCommon(HttpMethod method)
{
var body = ToJsonString(new SimpleReq
{
Key1 = "values",
Key2 = "values"
});
var request = new HttpRequestMessage(method, $"{host}/")
{
Content = new StringContent(body, Encoding.UTF8, "application/json"),
};
var resp = await client.SendAsync(request);
Console.WriteLine(await resp.Content.ReadAsStringAsync());
return true;
}
static void Main(string[] args)
{
var taskPost = RequestCommon(HttpMethod.Post);
taskPost.Wait();
}
}