本文整理汇总了C#中CredentialCache.Add方法的典型用法代码示例。如果您正苦于以下问题:C# CredentialCache.Add方法的具体用法?C# CredentialCache.Add怎么用?C# CredentialCache.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CredentialCache
的用法示例。
在下文中一共展示了CredentialCache.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UriAuthenticationTypeCredentialCache
private static CredentialCache UriAuthenticationTypeCredentialCache()
{
CredentialCache cc = new CredentialCache();
cc.Add(uriPrefix1, authenticationType1, credential1);
cc.Add(uriPrefix1, authenticationType2, credential2);
cc.Add(uriPrefix2, authenticationType1, credential3);
cc.Add(uriPrefix2, authenticationType2, credential4);
return cc;
}
示例2: CreateUriCredentialCacheCount
private static CredentialCacheCount CreateUriCredentialCacheCount(CredentialCache cc = null, int count = 0)
{
cc = cc ?? new CredentialCache();
cc.Add(uriPrefix1, authenticationType1, credential1); count++;
cc.Add(uriPrefix1, authenticationType2, credential2); count++;
cc.Add(uriPrefix2, authenticationType1, credential3); count++;
cc.Add(uriPrefix2, authenticationType2, credential4); count++;
return new CredentialCacheCount(cc, count);
}
示例3: GetCredential
private static CredentialCache GetCredential()
{
string url = @"http://github.com/api/v3/users";
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
CredentialCache credentialCache = new CredentialCache();
//credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(ConfigurationManager.AppSettings["gitHubUser"], ConfigurationManager.AppSettings["gitHubUserPassword"]));
credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential("huj", "Savit5ch"));
return credentialCache;
}
示例4: CreateHostPortCredentialCacheCount
private static CredentialCacheCount CreateHostPortCredentialCacheCount(CredentialCache cc = null, int count = 0)
{
cc = cc ?? new CredentialCache();
cc.Add(host1, port1, authenticationType1, credential1); count++;
cc.Add(host1, port1, authenticationType2, credential2); count++;
cc.Add(host1, port2, authenticationType1, credential3); count++;
cc.Add(host1, port2, authenticationType2, credential4); count++;
cc.Add(host2, port1, authenticationType1, credential5); count++;
cc.Add(host2, port1, authenticationType2, credential6); count++;
cc.Add(host2, port2, authenticationType1, credential7); count++;
cc.Add(host2, port2, authenticationType2, credential8); count++;
return new CredentialCacheCount(cc, count);
}
示例5: HostPortAuthenticationTypeCredentialCache
private static CredentialCache HostPortAuthenticationTypeCredentialCache()
{
CredentialCache cc = new CredentialCache();
cc.Add(host1, port1, authenticationType1, credential1);
cc.Add(host1, port1, authenticationType2, credential2);
cc.Add(host1, port2, authenticationType1, credential3);
cc.Add(host1, port2, authenticationType2, credential4);
cc.Add(host2, port1, authenticationType1, credential5);
cc.Add(host2, port1, authenticationType2, credential6);
cc.Add(host2, port2, authenticationType1, credential7);
cc.Add(host2, port2, authenticationType2, credential8);
return cc;
}
示例6: RequestWeibo
/// <summary>
/// 发送t_news到微博
/// </summary>
/// <param name="source">app key</param>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <param name="t_news">需要发送的微博内容</param>
/// <param name="conn">数据库连接</param>
public static void RequestWeibo(string source, string username, string password, string t_news, MySqlConnection conn)
{
string data = "source=" + source + "&status=" + HttpUtility.UrlEncode(t_news);
//准备用户验证数据
string usernamePassword = username + ":" + password;
//准备调用的URL及需要POST的数据
string url = "https://api.weibo.com/2/statuses/update.json";
//准备用于发起请求的HttpWebRequest对象
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
//准备用于用户验证的凭据
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
httpRequest.Credentials = myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
//发起POST请求
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
Encoding encoding = Encoding.ASCII;
byte[] bytesToPost = encoding.GetBytes(data);
httpRequest.ContentLength = bytesToPost.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();
//获取服务端的响应内容
try
{
WebResponse wr = httpRequest.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
receiveStream.Close();
}
catch (Exception e)
{
conn.Open();
MySqlCommand cmd2 = new MySqlCommand("insert into test(msg, date) values ('" + e.ToString() + "','" + DateTime.Now.ToString() + "')", conn);
cmd2.ExecuteNonQuery();
conn.Close();
}
}
示例7: CreateCredentialCache
private static CredentialCache CreateCredentialCache(int uriCount, int hostPortCount)
{
var cc = new CredentialCache();
for (int i = 0; i < uriCount; i++)
{
Uri uri = new Uri(UriPrefix + i.ToString());
cc.Add(uri, AuthenticationType, s_credential);
}
for (int i = 0; i < hostPortCount; i++)
{
string host = HostPrefix + i.ToString();
cc.Add(host, Port, AuthenticationType, s_credential);
}
return cc;
}
示例8: DoCredentialTest
public static void DoCredentialTest(string url)
{
var credential = new NetworkCredential();
credential.UserName = "skapila";
credential.Password = "blah" ;
var clientHandler = new HttpClientHandler();
var cc = new CredentialCache();
cc.Add(new Uri("http://httpbin.org"), "Basic", credential);
clientHandler.Credentials = cc;
clientHandler.PreAuthenticate = true;
var httpClient = new HttpClient(clientHandler);
httpClient.MaxResponseContentBufferSize = 256000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
try{
var responseTask = httpClient.GetAsync(url);
responseTask.Wait();
HttpResponseMessage response = responseTask.Result;
// response.EnsureSuccessStatusCode();
if(response.Content != null) {
var readTask = response.Content.ReadAsStreamAsync();
readTask.Wait();
Console.WriteLine(readTask.Result);
}
else
{
Console.WriteLine(response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine);
}
}
catch(Exception e)
{
Console.WriteLine(e);
return ;
}
return;
}
示例9: Submit
public HttpWebResponse Submit (Uri uri, FSpot.ProgressItem progress_item)
{
this.Progress = progress_item;
Request = (HttpWebRequest) WebRequest.Create (uri);
CookieCollection cookie_collection = Cookies.GetCookies (uri);
if (uri.UserInfo != null && uri.UserInfo != String.Empty) {
NetworkCredential cred = new NetworkCredential ();
cred.GetCredential (uri, "basic");
CredentialCache credcache = new CredentialCache();
credcache.Add(uri, "basic", cred);
Request.PreAuthenticate = true;
Request.Credentials = credcache;
}
Request.ServicePoint.Expect100Continue = expect_continue;
Request.CookieContainer = new CookieContainer ();
foreach (Cookie c in cookie_collection) {
if (SuppressCookiePath)
Request.CookieContainer.Add (new Cookie (c.Name, c.Value));
else
Request.CookieContainer.Add (c);
}
Request.Method = "POST";
Request.Headers["Accept-Charset"] = "utf-8;";
//Request.UserAgent = "F-Spot Gallery Remote Client";
Request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040626 Firefox/0.9.1";
Request.Proxy = WebProxy.GetDefaultProxy ();
if (multipart) {
GenerateBoundary ();
Request.ContentType = "multipart/form-data; boundary=" + boundary;
Request.Timeout = Request.Timeout * 3;
long length = 0;
for (int i = 0; i < Items.Count; i++) {
FormItem item = (FormItem)Items[i];
length += MultipartLength (item);
}
length += end_boundary.Length + 2;
//Request.Headers["My-Content-Length"] = length.ToString ();
if (Buffer == false) {
Request.ContentLength = length;
Request.AllowWriteStreamBuffering = false;
}
} else {
Request.ContentType = "application/x-www-form-urlencoded";
}
stream_writer = new StreamWriter (Request.GetRequestStream ());
first_item = true;
for (int i = 0; i < Items.Count; i++) {
FormItem item = (FormItem)Items[i];
Write (item);
}
if (multipart)
stream_writer.Write (end_boundary + "\r\n");
stream_writer.Flush ();
stream_writer.Close ();
HttpWebResponse response;
try {
response = (HttpWebResponse) Request.GetResponse ();
//Console.WriteLine ("found {0} cookies", response.Cookies.Count);
foreach (Cookie c in response.Cookies) {
Cookies.Add (c);
}
} catch (WebException e) {
if (e.Status == WebExceptionStatus.ProtocolError
&& ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.ExpectationFailed && expect_continue) {
e.Response.Close ();
expect_continue = false;
return Submit (uri, progress_item);
}
throw new WebException (Mono.Unix.Catalog.GetString ("Unhandled exception"), e);
}
return response;
}
示例10: Add_HostPortAuthenticationTypeCredential_DuplicateItem_Throws
public static void Add_HostPortAuthenticationTypeCredential_DuplicateItem_Throws()
{
CredentialCache cc = new CredentialCache();
cc.Add(host1, port1, authenticationType1, credential1);
Assert.Throws<ArgumentException>(() => cc.Add(host1, port1, authenticationType1, credential1));
}
示例11: Add_UriAuthenticationTypeCredential_DuplicateItem_Throws
public static void Add_UriAuthenticationTypeCredential_DuplicateItem_Throws()
{
CredentialCache cc = new CredentialCache();
cc.Add(uriPrefix1, authenticationType1, credential1);
Assert.Throws<ArgumentException>(() => cc.Add(uriPrefix1, authenticationType1, credential1));
}
示例12: GetCredential_SimilarUriAuthenticationType_GetLongestUriPrefix
public static void GetCredential_SimilarUriAuthenticationType_GetLongestUriPrefix()
{
CredentialCache cc = new CredentialCache();
cc.Add(new Uri("http://microsoft:80/greaterpath"), authenticationType1, credential2);
cc.Add(new Uri("http://microsoft:80/"), authenticationType1, credential1);
NetworkCredential nc = cc.GetCredential(new Uri("http://microsoft:80"), authenticationType1);
Assert.Equal(nc, credential2);
}
示例13: AddRemove_HostPortAuthenticationTypeDefaultCredentials_Success
public static void AddRemove_HostPortAuthenticationTypeDefaultCredentials_Success()
{
NetworkCredential nc = CredentialCache.DefaultNetworkCredentials as NetworkCredential;
CredentialCache cc = new CredentialCache();
cc.Add(host1, port1, authenticationType1, nc);
Assert.Equal(nc, cc.GetCredential(host1, port1, authenticationType1));
cc.Remove(host1, port1, authenticationType1);
Assert.Null(cc.GetCredential(host1, port1, authenticationType1));
}
示例14: CreateWebRequest
/// <summary>
/// This method creates secure/non secure web
/// request based on the parameters passed.
/// </summary>
/// <param name="uri"></param>
/// <param name="collHeader">This parameter of type
/// NameValueCollection may contain any extra header
/// elements to be included in this request </param>
/// <param name="RequestMethod">Value can POST OR GET</param>
/// <param name="NwCred">In case of secure request this would be true</param>
/// <returns></returns>
public virtual HttpWebRequest CreateWebRequest(string uri,
NameValueCollection collHeader,
string RequestMethod, bool NwCred)
{
HttpWebRequest webrequest =
(HttpWebRequest)WebRequest.Create(uri);
webrequest.KeepAlive = false;
webrequest.Method = RequestMethod;
int iCount = collHeader.Count;
string key;
string keyvalue;
for (int i = 0; i < iCount; i++)
{
key = collHeader.Keys[i];
keyvalue = collHeader[i];
webrequest.Headers.Add(key, keyvalue);
}
webrequest.ContentType = "text/html";
//"application/x-www-form-urlencoded";
if (ProxyServer.Length > 0)
{
webrequest.Proxy = new
WebProxy(ProxyServer, ProxyPort);
}
webrequest.AllowAutoRedirect = false;
if (NwCred)
{
CredentialCache wrCache =
new CredentialCache();
wrCache.Add(new Uri(uri), "Basic",
new NetworkCredential(UserName, UserPwd));
webrequest.Credentials = wrCache;
}
//Remove collection elements
collHeader.Clear();
return webrequest;
}//End of secure CreateWebRequest
示例15: GetAsync_CredentialIsCredentialCacheUriRedirect_StatusCodeOK
public async Task GetAsync_CredentialIsCredentialCacheUriRedirect_StatusCodeOK(int statusCode)
{
Uri uri = Configuration.Http.BasicAuthUriForCreds(secure:false, userName:Username, password:Password);
Uri redirectUri = Configuration.Http.RedirectUriForCreds(
secure:false,
statusCode:statusCode,
userName:Username,
password:Password);
_output.WriteLine(uri.AbsoluteUri);
_output.WriteLine(redirectUri.AbsoluteUri);
var credentialCache = new CredentialCache();
credentialCache.Add(uri, "Basic", _credential);
var handler = new HttpClientHandler();
handler.Credentials = credentialCache;
using (var client = new HttpClient(handler))
{
using (HttpResponseMessage response = await client.GetAsync(redirectUri))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(uri, response.RequestMessage.RequestUri);
}
}
}