/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
namespace Everest.Library.Net{
public class HtmlRequest
{
public HtmlRequest()
{
UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; Zune 2.0; CIBA; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*";
}
CookieContainer cookieContainer = new CookieContainer();
string cookiesHeader = "";
public string UserAgent { get; set; }
public string Accept { get; set; }
public string Request(string url, NameValueCollection parameters, string method, out HttpStatusCode statusCode)
{
return Request(url, parameters, method, "application/x-www-form-urlencoded", Encoding.Default, out statusCode);
}
public string Request(string url, NameValueCollection parameters, string method, string contentType, Encoding encoding, out HttpStatusCode statusCode)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = contentType;
request.Method = method;
request.UserAgent = UserAgent;
request.Accept = Accept;
request.CookieContainer = cookieContainer;
request.Headers.Add("cookie:" + cookiesHeader);
if (parameters != null && parameters.Keys.Count > 0)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (string key in parameters.Keys)
{
stringBuilder.AppendFormat("{0}={1}&", key, parameters[key]);
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
byte[] setStr = System.Text.Encoding.Default.GetBytes(stringBuilder.ToString());
request.ContentLength = setStr.Length;
System.IO.Stream s = request.GetRequestStream();
s.Write(setStr, 0, setStr.Length);
s.Close();
}
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
if (string.IsNullOrEmpty(cookiesHeader))
{
cookiesHeader = cookieContainer.GetCookieHeader(new Uri(url));
}
System.IO.StreamReader sr = new System.IO.StreamReader(httpWebResponse.GetResponseStream(), encoding);
string outPut = sr.ReadToEnd();
//this.TextBox2.Text = outPut;
sr.Close();
statusCode = httpWebResponse.StatusCode;
return outPut;
}
}
}
|