Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- 자바
- scrollView
- Bootstrap
- SpringSource Tool Suite
- Apache Lucene
- 이클립스
- 웹뷰
- MS-SQL
- Maven
- STS
- javascript
- Redirect
- Android
- WebView
- MSsql
- MANTIS
- 안드로이드
- C#
- decompiler
- varags
- TextBox
- Java
- 자바스크립트
- Web Service
- Eclipse
- 컬럼명
- html
- jsp
- asp.net
- 웹 서비스
Archives
- Today
- Total
bboks.net™
C# HTTP Get 라이브러리 본문
C#에서 웹 페이지의 Reqest를 요청하고 그 결과를 반환하는 클래스
사용법:
[출처] C# .NET 2.0 HTTP GET Class
public class HttpGet
{
private HttpWebRequest request;
private HttpWebResponse response;
private string responseBody;
private string escapedBody;
private int statusCode;
private double responseTime;
public string ResponseBody { get { return responseBody; } }
public string EscapedBody { get { return GetEscapedBody(); } }
public int StatusCode { get { return statusCode; } }
public double ResponseTime { get { return responseTime; } }
public string Headers { get { return GetHeaders(); } }
public string StatusLine { get { return GetStatusLine(); } }
public void Request(string url)
{
Stopwatch timer = new Stopwatch();
StringBuilder respBody = new StringBuilder();
this.request = (HttpWebRequest)WebRequest.Create(url);
try
{
timer.Start();
this.response = (HttpWebResponse)this.request.GetResponse();
byte[] buf = new byte[8192];
Stream respStream = this.response.GetResponseStream();
int count = 0;
do
{
count = respStream.Read(buf, 0, buf.Length);
if (count != 0)
respBody.Append(Encoding.ASCII.GetString(buf, 0, count));
}
while (count > 0);
timer.Stop();
this.responseBody = respBody.ToString();
this.statusCode = (int)(HttpStatusCode)this.response.StatusCode;
this.responseTime = timer.ElapsedMilliseconds / 1000.0;
}
catch (WebException ex)
{
this.response = (HttpWebResponse)ex.Response;
this.responseBody = "No Server Response";
this.escapedBody = "No Server Response";
this.responseTime = 0.0;
}
}
private string GetEscapedBody()
{ // HTML escaped chars
string escapedBody = responseBody;
escapedBody = escapedBody.Replace("&", "&");
escapedBody = escapedBody.Replace("<", "<");
escapedBody = escapedBody.Replace(">", ">");
escapedBody = escapedBody.Replace("'", "'");
escapedBody = escapedBody.Replace("\"", """);
this.escapedBody = escapedBody;
return escapedBody;
}
private string GetHeaders()
{
if (response == null)
return "No Server Response";
else
{
StringBuilder headers = new StringBuilder();
for (int i = 0; i < this.response.Headers.Count; ++i)
headers.Append(String.Format("{0}: {1}\n",
response.Headers.Keys[i], response.Headers[i]));
return headers.ToString();
}
}
private string GetStatusLine()
{
if (response == null)
return "No Server Response";
else
return String.Format("HTTP/{0} {1} {2}", response.ProtocolVersion,
(int)response.StatusCode, response.StatusDescription);
}
}
사용법:
public class Program
{
static void Main(string[] args)
{
HTTPGet req = new HTTPGet();
req.Request("http://www.google.com");
Console.WriteLine(req.StatusLine);
Console.WriteLine(req.ResponseTime);
Console.WriteLine(req.ResponseBody);
}
}
[출처] C# .NET 2.0 HTTP GET Class