using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace WebManager
{
class WebConstants
{
public static readonly string ContentType = "application/x-www-form-urlencoded";
public static readonly string UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7";
public static readonly string Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
}
public class WebSession
{
private string main_site;
private CookieContainer cook;
public string user_agent;
public string referer;
public WebSession()
{
cook = new CookieContainer();
}
public WebSession(string site)
: this()
{
main_site = site;
user_agent = WebConstants.UserAgent;
referer = "";
}
public string SendRequest(string path, string method, string options)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(main_site + path);
req.Method = method;
req.CookieContainer = cook;
req.UserAgent = user_agent;
if (method == "POST")
{
req.ContentType = WebConstants.ContentType;
req.ContentLength = options.Length;
Console.WriteLine(options);
StreamWriter sw = new StreamWriter(req.GetRequestStream());
sw.Write(options);
sw.Close();
}
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
Console.WriteLine(req.HaveResponse);
StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding(1251));
string result = sr.ReadToEnd();
sr.Close();
cook = req.CookieContainer;
return result;
}
}
}
c# HttpWebRequest
Вот код класса отправки\принятия сообщений:
Код:
запускаю я его просто:
Код:
namespace Debug
{
class Program
{
static void Main(string[] args)
{
WebSession sess = new WebSession("http://heroeswm.ru");
string res = sess.SendRequest("", "GET", "");
Console.WriteLine(res);
}
}
}
{
class Program
{
static void Main(string[] args)
{
WebSession sess = new WebSession("http://heroeswm.ru");
string res = sess.SendRequest("", "GET", "");
Console.WriteLine(res);
}
}
}
На с# пишу недавно, очень прошу разобраться, приму любую критику))