Referer : http://codesamplez.com/programming/http-request-c-sharp
===============
public class MyWebRequest { private WebRequest request; private Stream dataStream; private string status; public String Status { get { return status; } set { status = value; } } public MyWebRequest(string url) { // Create a request using a URL that can receive a post. request = WebRequest.Create(url); } public MyWebRequest(string url, string method) : this(url) { if (method.Equals("GET") || method.Equals("POST")) { // Set the Method property of the request to POST. request.Method = method; } else { throw new Exception("Invalid Method Type"); } } public MyWebRequest(string url, string method, string data) : this(url, method) { // Create POST data and convert it to a byte array. string postData = data; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); } public string GetResponse() { // Get the original response. WebResponse response = request.GetResponse(); this.Status = ((HttpWebResponse)response).StatusDescription; // Get the stream containing all content returned by the requested server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content fully up to the end. string responseFromServer = reader.ReadToEnd(); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } }
===============
사용법
==============
//create the constructor with post type and few data MyWebRequest myRequest = new MyWebRequest("http://www.yourdomain.com","POST","a=value1&b=value2"); //show the response string on the console screen. Console.WriteLine(myRequest.GetResponse());
==============
'삽질이라쓰고_경험이라_읽는다.' 카테고리의 다른 글
안드로이드 usb드라이버 - MTP 실패시 (0) | 2014.05.13 |
---|---|
Attack and Discovery Pattern Database for Application Fuzz Testing (0) | 2012.11.19 |
국가별 OS,브라우져 점유율 (gs.statcounter.com) (0) | 2012.02.16 |
[패스워드 크랙] rarcrack 사용방법 (0) | 2011.12.28 |
Meaning of ASCII CHARACTER TABLE (0) | 2011.11.04 |