티스토리 뷰
using System;
using System.Net;
using System.Web;
using System.Collections;
using System.IO;
// No .NET4 Client Profile
// Add reference System.Web
namespace WebRequestExample
{
class WebPostRequest
{
WebRequest theRequest;
HttpWebResponse theResponse;
ArrayList theQueryData;
public WebPostRequest(string url)
{
theRequest = WebRequest.Create(url);
theRequest.Method = "POST";
theQueryData = new ArrayList();
}
public void Add(string key, string value)
{
theQueryData.Add(String.Format("{0}={1}", key, System.Web.HttpUtility.UrlEncode(value)));
}
public string GetResponse()
{
// Set the encoding type
theRequest.ContentType = "application/x-www-form-urlencoded";
// Build a string containing all the parameters
string Parameters = String.Join("&", (String[])theQueryData.ToArray(typeof(string)));
theRequest.ContentLength = Parameters.Length;
// We write the parameters into the request
StreamWriter sw = new StreamWriter(theRequest.GetRequestStream());
sw.Write(Parameters);
sw.Close();
// Execute the query
theResponse = (HttpWebResponse)theRequest.GetResponse();
StreamReader sr = new StreamReader(theResponse.GetResponseStream());
return sr.ReadToEnd();
}
}
class MainClass
{
public static void Main(string[] args)
{
//WebPostRequest myPost = new WebPostRequest("http://www.dijksterhuis.org/test/post.php");
WebPostRequest myPost = new WebPostRequest("http://www.naver.com");
myPost.Add("keyword", "void");
myPost.Add("data", "hello&+-[]");
Console.WriteLine(myPost.GetResponse());
}
}
}
'프로그래밍' 카테고리의 다른 글
Node.js Ubuntu에 설치하기 (0) | 2016.04.06 |
---|---|
Visual C++ Win32 타이머 CreateTimerQueueTimer 예제 (0) | 2016.04.06 |
Visual Studio 2013에서 MFC가 컴파일되지 않을때 (0) | 2016.04.06 |
C# PasrseHTML로 HTML 파싱하기 (0) | 2016.04.06 |
C# 서버개발 관련 마영전 정리 (0) | 2016.04.06 |
- Total
- Today
- Yesterday