本文整理汇总了C#中CookieContainer类的典型用法代码示例。如果您正苦于以下问题:C# CookieContainer类的具体用法?C# CookieContainer怎么用?C# CookieContainer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CookieContainer类属于命名空间,在下文中一共展示了CookieContainer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPage
public string GetPage(string posturl)
{
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
Response.Write(content);
return content;
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}
示例2: Capacity_Set_Invalid
public static void Capacity_Set_Invalid()
{
CookieContainer cc = new CookieContainer();
Assert.Throws<ArgumentOutOfRangeException>(() => cc.Capacity = 0); // <= 0
Assert.Throws<ArgumentOutOfRangeException>(() => cc.Capacity = cc.PerDomainCapacity - 1); // < per domain capacity
}
示例3: GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains
public void GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie1 = new Cookie(CookieName1, CookieValue1) { Domain = OriginalDomain };
container.Add(new Uri(SchemePrefix + OriginalDomain), cookie1);
var uri = new Uri(SchemePrefix + OriginalDomain);
var cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
Assert.Equal(OriginalDomain, cookies[CookieName1].Domain);
uri = new Uri(SchemePrefix + "www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
}
示例4: CreateCount11Container
private static CookieContainer CreateCount11Container()
{
CookieContainer cc1 = new CookieContainer();
// Add(Cookie)
cc1.Add(c1);
cc1.Add(c2);
cc1.Add(c3);
cc1.Add(c4);
// Add(CookieCollection)
CookieCollection cc2 = new CookieCollection();
cc2.Add(c5);
cc2.Add(c6);
cc2.Add(c7);
cc1.Add(cc2);
// Add(Uri, Cookie)
cc1.Add(u4, c8);
cc1.Add(u4, c9);
// Add(Uri, CookieCollection)
cc2 = new CookieCollection();
cc2.Add(c10);
cc2.Add(c11);
cc1.Add(u5, cc2);
return cc1;
}
示例5: GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch
public void GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie1 = new Cookie(CookieName1, CookieValue1);
var cookie2 = new Cookie(CookieName2, CookieValue2) { Version = 1 };
var uri = new Uri(SchemePrefix + OriginalDomain);
container.Add(uri, cookie1);
container.Add(uri, cookie2);
var cookies = container.GetCookies(uri);
Assert.Equal(2, cookies.Count);
Assert.Equal(OriginalDomain, cookies[CookieName1].Domain);
Assert.Equal(OriginalDomain, cookies[CookieName2].Domain);
uri = new Uri(SchemePrefix + "www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
}
示例6: CookiePortTest
public CookiePortTest()
{
_cc = new CookieContainer();
_cookie = new Cookie("name", "value1", "/path", "localhost");
// use both space and comma as delimiter
_cookie.Port = "\"80 110,1050, 1090 ,1100\"";
_cc.Add(new Uri("http://localhost/path"), _cookie);
}
示例7: AddCookieCollection_Success
public static void AddCookieCollection_Success()
{
CookieContainer cc = new CookieContainer();
CookieCollection cookieCollection = new CookieCollection();
cookieCollection.Add(new Cookie("name3", "value","/",".contoso.com"));
cc.Add(cookieCollection);
Assert.Equal(1, cc.Count);
}
示例8: GetResponseByRequest
public GetResponseByRequest(string url, string data, CookieContainer ccIn)
{
this.url = url;
this.sendData = data;
this.cc = ccIn;
this.requestTimeout = 120000; //iki deqiqe;
}
示例9: MaxCookieSize_GetSet_Success
public static void MaxCookieSize_GetSet_Success()
{
CookieContainer cc = new CookieContainer();
Assert.Equal(CookieContainer.DefaultCookieLengthLimit, cc.MaxCookieSize);
cc.MaxCookieSize = 8192;
Assert.Equal(8192, cc.MaxCookieSize);
}
示例10: Ctor_CapacityPerDomainCapacityMaxCookieSize_Success
public static void Ctor_CapacityPerDomainCapacityMaxCookieSize_Success()
{
CookieContainer cc = new CookieContainer(5, 4, 3);
Assert.Equal(5, cc.Capacity);
Assert.Equal(4, cc.PerDomainCapacity);
Assert.Equal(3, cc.MaxCookieSize);
cc = new CookieContainer(10, int.MaxValue, 4); // Even though PerDomainCapacity > Capacity, this shouldn't throw
}
示例11: EndSession
static void EndSession (CookieContainer container)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://localhost:8081/end.aspx");
request.CookieContainer = container;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
response.Close ();
}
示例12: Add_CookieVersion1AndRootDomainWithNoLeadingDot_ThrowsCookieException
public void Add_CookieVersion1AndRootDomainWithNoLeadingDot_ThrowsCookieException()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie = new Cookie(CookieName1, CookieValue1) { Version = 1, Domain = OriginalDomain };
var uri = new Uri(SchemePrefix + OriginalDomain);
Assert.Throws<CookieException>(() => container.Add(uri, cookie));
}
示例13: RequisicaoRV
public DataPrev.Domain.SistemaModel.ErroModel RequisicaoRV(long NumeroBeneficio)
{
DataPrev.Domain.SistemaModel.ErroModel Retorno = new Domain.SistemaModel.ErroModel();
try
{
Domain.SistemaModel.IpModel IpList = new Domain.SistemaModel.IpModel();
if (System.Configuration.ConfigurationManager.AppSettings["Consulta"] == "Lote")
IpList = Rep.IpsLiberados();
if (System.Configuration.ConfigurationManager.AppSettings["Consulta"] == "Sistema")
IpList = Rep.IpsLiberados2();
WebProxy proxyObj = new WebProxy(IpList.IP, IpList.Porta);
proxyObj.Credentials = CredentialCache.DefaultCredentials;
Retorno.Retorno3 = IpList.IP;
Uri link = new Uri("http://www010.dataprev.gov.br/CWS/BIN/CWS.asp");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(link);
request.ProtocolVersion = HttpVersion.Version10;
request.Proxy = proxyObj;
request.Credentials = CredentialCache.DefaultCredentials;
CookieContainer _Cookies = new CookieContainer();
request.CookieContainer = _Cookies;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Referer = "http://www3.dataprev.gov.br/cws/contexto/hiscre/hiscrenet2.asp";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.UTF8.GetBytes("C_1=BLR00.11&C_2=&C_3=" + NumeroBeneficio + "&layout=8%2C69%2C10%2C8%2C1&submit=Transmite");
request.ContentLength = byteArray.Length;
var h = request.CookieContainer.GetCookies(request.RequestUri);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
int d = dataStream.ReadTimeout;
dataStream.Close();
//Fonte extrato retorno
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
Retorno.Fonte = reader.ReadToEnd();
response.Close();
request.Abort();
}
catch (Exception e)
{
Retorno.Fonte = e.Message;
}
Thread.Sleep(20000);
return Retorno;
}
示例14: Capacity_GetSet_Success
public static void Capacity_GetSet_Success()
{
CookieContainer cc = new CookieContainer();
Assert.Equal(CookieContainer.DefaultCookieLimit, cc.Capacity);
cc.Capacity = 900;
Assert.Equal(900, cc.Capacity);
cc.Capacity = 40; //Shrink
Assert.Equal(40, cc.Capacity);
}
示例15: Main
static int Main ()
{
CookieContainer cookies = new CookieContainer ();
HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://localhost:8081/Default.aspx");
request.CookieContainer = cookies;
request.Method = "GET";
try {
HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
using (StreamReader sr = new StreamReader (response.GetResponseStream (), Encoding.UTF8, true)) {
string result = sr.ReadToEnd ();
Assert.IsTrue (result.IndexOf ("<p>1</p>") != -1, "#A:" + result);
}
response.Close ();
} catch (WebException ex) {
HttpWebResponse response = (HttpWebResponse) ex.Response;
if (response != null) {
using (StreamReader sr = new StreamReader (response.GetResponseStream ())) {
Console.WriteLine (sr.ReadToEnd ());
}
}
return 1;
}
request = (HttpWebRequest) WebRequest.Create ("http://localhost:8081/Default.aspx");
request.CookieContainer = cookies;
request.Method = "GET";
try {
HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
using (StreamReader sr = new StreamReader (response.GetResponseStream (), Encoding.UTF8, true)) {
string result = sr.ReadToEnd ();
Assert.IsTrue (result.IndexOf ("<p>2</p>") != -1, "#B:" + result);
}
response.Close ();
} catch (WebException ex) {
HttpWebResponse response = (HttpWebResponse) ex.Response;
if (response != null) {
using (StreamReader sr = new StreamReader (response.GetResponseStream ())) {
Console.WriteLine (sr.ReadToEnd ());
}
}
return 2;
}
return 0;
}