출처 : http://rest.elkstein.org/2008/02/using-rest-in-c-sharp.html






using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
public class WebApiUtil
{
public static bool HttpGet(string url, out string Msg)
{
// SSL 사용시에 SSL 인증서가 맞지 않더라도 통과하게 하는 부분, 테스트시에만 사용하고 제품에는 나가지 않도록 하는 것이 좋다
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

bool _flag = false;
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
if (resp.StatusCode == HttpStatusCode.OK)
{
_flag = true;
}
StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
Msg = reader.ReadToEnd();
}
return _flag;
}
public static bool HttpPost(string url, Dictionary<string, string> paramList, out string Msg)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

bool _flag = false;
HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
// Build a string with all the params, properly encoded.
// We assume that the arrays paramName and paramVal are
// of equal length:
StringBuilder paramz = new StringBuilder();
foreach (KeyValuePair<string, string> param in paramList)
{
paramz.Append(param.Key);
paramz.Append("=");
paramz.Append(System.Web.HttpUtility.UrlEncode(param.Value));
paramz.Append("&");
}
// Encode the parameters as form data:
byte[] formData = UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;
// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
post.Flush();
}
// Pick up the response:
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
if (resp.StatusCode == HttpStatusCode.OK)
{
_flag = true;
}
StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
Msg = reader.ReadToEnd();
}
return _flag;
}
public static bool HttpPost(string url, object obj, out string Msg)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

bool _flag = false;
HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
// Build a string with all the params, properly encoded.
// We assume that the arrays paramName and paramVal are
// of equal length:
string jsonString = new JavaScriptSerializer().Serialize(obj);
StringBuilder paramz = new StringBuilder();
paramz.Append("param=");
paramz.Append(System.Web.HttpUtility.UrlEncode(jsonString));
// Encode the parameters as form data:
byte[] formData = UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;
// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
post.Flush();
}
// Pick up the response:
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
if (resp.StatusCode == HttpStatusCode.OK)
{
_flag = true;
}
StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
Msg = reader.ReadToEnd();
}
return _flag;
}

private string paramzToJson(IDictionary<string, string> paramList)
{
StringBuilder paramz = new StringBuilder();
paramz.Append("{");
foreach (KeyValuePair<string, string> param in paramList)
{
if (paramz.Length > 1)
paramz.Append(" ,");
paramz.Append(String.Format("\"{0}\"", param.Key));
paramz.Append(" : ");
paramz.Append(String.Format("\"{0}\"", System.Web.HttpUtility.UrlEncode(param.Value)));
}
paramz.Append("}");
return paramz.ToString();
}

}






























Posted by motolies
,