本文整理汇总了C#中System.Net.CookieContainer.SetCookies方法的典型用法代码示例。如果您正苦于以下问题:C# CookieContainer.SetCookies方法的具体用法?C# CookieContainer.SetCookies怎么用?C# CookieContainer.SetCookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.CookieContainer
的用法示例。
在下文中一共展示了CookieContainer.SetCookies方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: process
public int process(CookieContainer cc)
{
System.Uri uri = null;
string url = null;
int res;
res = pacifica_auth(mycallback);
if(res == 0)
{
string test = System.Text.Encoding.ASCII.GetString(cookiedata);
string[] lines = test.Split(new Char[] {'\n', '\r'});
foreach(string lineraw in lines)
{
string line = lineraw;
int cmplen = "URL:".Length;
if(line.Length >= cmplen && line.Substring(0, cmplen).ToLower().Equals("URL:".ToLower()))
{
url = lineraw.Substring(cmplen, lineraw.Length - cmplen).Trim();
uri = new System.Uri(url);
continue;
}
cmplen = "Set-Cookie:".Length;
if(line.Length >= cmplen && line.Substring(0, cmplen).ToLower().Equals("Set-Cookie:".ToLower()))
{
line = lineraw.Substring(cmplen, lineraw.Length - cmplen).Trim();
cc.SetCookies(uri, line);
}
}
}
return res;
}
示例2: Login
public async Task<bool> Login(CancellationToken cancellationToken = default(CancellationToken))
{
if (IsLoogedIn)
return true;
var getConfigJob = ConfigurationService.GetClientConfig();
var credentialsStr = Configuration.Username + ":" + Configuration.Password;
var credentials64Str = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentialsStr));
var authUri = new Uri((await getConfigJob).ApiConfig.BaseUrl,
_apiUri.Replace("{username}", Configuration.Username));
var authRequest = await CreateRequest(authUri);
authRequest.Method = HttpMethod.Post;
authRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials64Str);
var response = await SendRequest(authRequest);
var cookies = new CookieContainer();
cookies.SetCookies(authUri, String.Join(", ",response.Headers.GetValues("Set-Cookie")));
SessionId = cookies.GetCookies(new Uri("http://gpodder.net"))["sessionid"];
#if DEBUG
// if (SessionId != null)
//Console.WriteLine("Authenticated");
#endif
return true;
}
示例3: GetUriCookieContainer
public static CookieContainer GetUriCookieContainer(Uri uri)
{
CookieContainer l_Cookies = null;
// Determine the size of the cookie
int l_Datasize = 8192 * 16;
StringBuilder l_CookieData = new StringBuilder(l_Datasize);
if (!InternetGetCookieEx(uri.ToString(), null, l_CookieData, ref l_Datasize, InternetCookieHttponly, IntPtr.Zero))
{
if (l_Datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
l_CookieData = new StringBuilder(l_Datasize);
if (!InternetGetCookieEx(uri.ToString(), null, l_CookieData, ref l_Datasize, InternetCookieHttponly, IntPtr.Zero))
return null;
}
if (l_CookieData.Length > 0)
{
l_Cookies = new CookieContainer();
l_Cookies.SetCookies(uri, l_CookieData.ToString().Replace(';', ','));
}
return l_Cookies;
}
示例4: Gethtml
public static string Gethtml(string URL, string cookie)
{
HttpWebRequest httpWebRequest;
WebResponse wrp;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
CookieContainer co = new CookieContainer();
Uri uri = new Uri(URL);
co.SetCookies(uri, cookie);
httpWebRequest.CookieContainer = co;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept =
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "get";
wrp = (HttpWebResponse)httpWebRequest.GetResponse();
string html = new StreamReader(wrp.GetResponseStream(), Encoding.UTF8).ReadToEnd();
cookie = wrp.Headers.Get("Set-Cookie");
return cookie+"\r\n\r\n\r\n=========================\r\n\r\n\r\n"+html;
}
示例5: SendHttpPostRequest
/// <summary>
/// ͬ������HttpRequest
/// </summary>
/// <param name="cookie"></param>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <returns></returns>
public static HttpWebResponse SendHttpPostRequest(Cookie cookie, string url, string postData)
{
//���https�µ�֤������
HttpRequestCredentialHelper.SetDefaultCredentialValidationLogic();
var request = HttpWebRequest.Create(url) as HttpWebRequest;
//������������ΪPOST
request.Method = "POST";
//����Post������
if (!string.IsNullOrEmpty(postData))
{
request.ContentLength = postData.Length;
request.ContentType = "application/x-www-form-urlencoded";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(postData);
writer.Close();
}
}
//��Cookie�����������÷�����֪����ǰ�û������
var container = new CookieContainer();
request.CookieContainer = container;
if (cookie != null)
{
container.SetCookies(new Uri(Constants.ROOT_URL), string.Format("{0}={1}", cookie.Name, cookie.Value));
var logger = DependencyResolver.Resolve<ILoggerFactory>().Create(typeof(HttpWebRequestHelper));
logger.InfoFormat("HttpWebRequest CookieName:{0}, Value:{1}", cookie.Name, cookie.Value);
}
return request.GetResponse() as HttpWebResponse;
}
示例6: GetHtml
public static string GetHtml(string URL, string cookie, out string header, string server)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
string getString = "";
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.Accept = "*/*";
httpWebRequest.Referer = "http://yueche.woiche.com/";
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(server), cookie);
httpWebRequest.CookieContainer = co;
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "GET";
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.UTF8);
getString = streamReader.ReadToEnd();
streamReader.Close();
getStream.Close();
return getString;
}
示例7: GetHtmlByBytes
public static byte[] GetHtmlByBytes(string host, string URL, byte[] byteRequest,string cookie, out string header)
{
long contentLength; // 获取请求返回的内容的长度
HttpWebResponse webResponse;
Stream getStream;
HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(host), cookie);
request.CookieContainer = co;
InitialHeadInfo(request);
request.ContentLength = byteRequest.Length;
Stream stream;
stream = request.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
webResponse = request.GetResponse() as HttpWebResponse;
contentLength = webResponse.ContentLength;
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
byte[] outBytes = new byte[5000];
outBytes = ReadFully(getStream);
getStream.Close();
request = null;
webResponse = null;
return outBytes;
}
示例8: GetUriCookieContainer
// This function returns cookie data based on a uniform resource identifier
public static CookieContainer GetUriCookieContainer(Uri uri)
{
// First, create a null cookie container
CookieContainer cookies = null;
// Determine the size of the cookie
var datasize = 8192 * 16;
var cookieData = new StringBuilder(datasize);
// Call InternetGetCookieEx from wininet.dll
if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(
uri.ToString(),
null, cookieData,
ref datasize,
InternetCookieHttponly,
IntPtr.Zero))
return null;
}
// If the cookie contains data, add it to the cookie container
if (cookieData.Length > 0)
{
cookies = new CookieContainer();
cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
}
// Return the cookie container
return cookies;
}
示例9: CookieContainer
public override string this[string name]
{
get
{
return this.innerCollection[name];
}
set
{
if ((name != "Content-Length") && (name != "Accept-Charset"))
{
if (name == "Cookie")
{
if (this.request != null)
{
CookieContainer container = new CookieContainer();
container.SetCookies(this.request.RequestUri, value);
this.request.CookieContainer = container;
}
else
{
this.innerCollection[name] = value;
}
}
else
{
this.innerCollection[name] = value;
}
}
}
}
示例10: GetIECookies
/// <summary>
/// Gets the URI cookie container.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns></returns>
internal static CookieContainer GetIECookies(Uri uri)
{
CookieContainer cookies = null;
try
{
// Determine the size of the cookie
int datasize = 8192 * 16;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, (uint)InternetFlags.INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(
uri.ToString(),
null, cookieData,
ref datasize,
(uint)InternetFlags.INTERNET_COOKIE_HTTPONLY,
IntPtr.Zero))
return null;
}
if (cookieData.Length > 0)
{
cookies = new CookieContainer();
cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
}
}
catch { }
return cookies;
}
示例11: ImportCookiesFromIE
public static CookieContainer ImportCookiesFromIE()
{
var cookie = new CookieContainer();
string targetUrl;
string plusCookie;
targetUrl = "https://plus.google.com";
plusCookie = GetProtectedModeIECookieValue(targetUrl).Replace(';', ',');
cookie.SetCookies(new Uri(targetUrl), plusCookie);
targetUrl = "https://accounts.google.com";
plusCookie = GetProtectedModeIECookieValue(targetUrl).Replace(';', ',');
cookie.SetCookies(new Uri(targetUrl), plusCookie);
targetUrl = "https://talkgadget.google.com";
plusCookie = GetProtectedModeIECookieValue(targetUrl).Replace(';', ',');
cookie.SetCookies(new Uri(targetUrl), plusCookie);
return cookie;
}
示例12: GetCookieContainerForUrl
public static CookieContainer GetCookieContainerForUrl(Uri url)
{
CookieContainer container = new CookieContainer();
string cookieHeaders = RetrieveIECookiesForUrl(url.AbsoluteUri);
if (cookieHeaders.Length > 0)
{
try { container.SetCookies(url, cookieHeaders); }
catch (CookieException) { }
}
return container;
}
示例13: GetUriCookieContainer
public static CookieContainer GetUriCookieContainer(Uri uri)
{
CookieContainer cookies = null;
string cookieString = GetCookieString(uri);
if (cookieString.Length > 0)
{
cookies = new CookieContainer();
cookies.SetCookies(uri, cookieString.Replace(';', ','));
}
return cookies;
}
示例14: ExtractAuthCookiesFromUrl
public static CookieCollection ExtractAuthCookiesFromUrl(string url)
{
Uri uriBase = new Uri(url);
Uri uri = new Uri(uriBase, "/");
// call WinInet.dll to get cookie.
string stringCookie = CookieReader.GetCookie(uri.ToString());
if (string.IsNullOrEmpty(stringCookie)) return null;
stringCookie = stringCookie.Replace("; ", ",").Replace(";", ",");
// use CookieContainer to parse the string cookie to CookieCollection
CookieContainer cookieContainer = new CookieContainer();
cookieContainer.SetCookies(uri, stringCookie);
return cookieContainer.GetCookies(uri);
}
示例15: GetUriCookieContainer
/// <summary>
/// Gets the URI cookie container.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns></returns>
public static CookieContainer GetUriCookieContainer(Uri uri)
{
CookieContainer cookies = null;
// Determine the size of the cookie
string cookieData = GetUriCookies(uri);
if (cookieData.Length > 0)
{
cookies = new CookieContainer();
cookies.SetCookies(uri, cookieData.Replace(';', ','));
}
return cookies;
}