本文整理汇总了C#中RestClient.AddHeader方法的典型用法代码示例。如果您正苦于以下问题:C# RestClient.AddHeader方法的具体用法?C# RestClient.AddHeader怎么用?C# RestClient.AddHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestClient
的用法示例。
在下文中一共展示了RestClient.AddHeader方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HammockHttpClient
public HammockHttpClient(Uri baseUri, string username, string password)
{
this.client = new RestClient { Authority = baseUri.ToString() };
client.AddHeader("Accept", "application/json");
client.AddHeader("Content-Type", "application/json; charset=utf-8");
client.ServicePoint = System.Net.ServicePointManager.FindServicePoint(baseUri);
client.ServicePoint.SetTcpKeepAlive(true, 300, 30);
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
var credentials = username + ":" + password;
var base64Credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
client.AddHeader("Authorization", "Basic " + base64Credentials);
}
client.RetryPolicy = new RetryPolicy
{
RetryConditions =
{
new Hammock.Retries.NetworkError(),
new Hammock.Retries.Timeout(),
new Hammock.Retries.ConnectionClosed()
},
RetryCount = 3
};
client.BeforeRetry += new EventHandler<RetryEventArgs>(client_BeforeRetry);
}
示例2: HammockHttpClient
public HammockHttpClient(Uri baseUri, string username, string password, TimeSpan timeout, bool shouldInitConnection)
{
this.client = new RestClient { Authority = baseUri.ToString() };
client.AddHeader("Accept", "application/json");
client.AddHeader("Content-Type", "application/json; charset=utf-8");
client.ServicePoint = System.Net.ServicePointManager.FindServicePoint(baseUri);
#if ! MONO
client.ServicePoint.SetTcpKeepAlive(true, 300, 30);
#endif
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
var credentials = username + ":" + password;
var base64Credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
client.AddHeader("Authorization", "Basic " + base64Credentials);
}
client.Timeout = timeout;
client.RetryPolicy = new RetryPolicy
{
RetryConditions =
{
new Hammock.Retries.NetworkError(),
new Hammock.Retries.Timeout(),
new Hammock.Retries.ConnectionClosed()
},
RetryCount = 3
};
client.BeforeRetry += new EventHandler<RetryEventArgs>(client_BeforeRetry);
//The first time a request is made to a URI, the ServicePointManager
//will create a ServicePoint to manage connections to a particular host
//This process is expensive and slows down the first created view.
//The call to BeginRequest is basically an async, no-op HTTP request to
//initialize the ServicePoint before the first view request is made.
if (shouldInitConnection) client.BeginRequest();
}
示例3: HammockHttpClient
public HammockHttpClient(Uri baseUri)
{
this.client = new RestClient { Authority = baseUri.ToString() };
client.AddHeader("Accept", "application/json");
client.AddHeader("Content-Type", "application/json; charset=utf-8");
client.ServicePoint = System.Net.ServicePointManager.FindServicePoint(baseUri);
client.ServicePoint.SetTcpKeepAlive(true, 300, 30);
client.RetryPolicy = new RetryPolicy
{
RetryConditions =
{
new Hammock.Retries.NetworkError(),
new Hammock.Retries.Timeout(),
new Hammock.Retries.ConnectionClosed()
},
RetryCount = 3
};
client.BeforeRetry += new EventHandler<RetryEventArgs>(client_BeforeRetry);
}
示例4: UploadString
/// <summary>
/// 上传数据
/// </summary>
/// <param name="url"></param>
/// <param name="service"></param>
/// <param name="queries"></param>
/// <param name="action"></param>
/// <param name="failed"></param>
protected void UploadString(string url
, IDictionary<string, string> queries
, Action<RestResponse, object> action
, Action<Exception> failed
, object userState)
{
bool httpResult = HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
RestClient client = new RestClient();
client.Method = WebMethod.Post;
queries.ToList().ForEach(o => client.AddParameter(o.Key, o.Value));
client.AddHeader("X-Requested-With", "xmlhttp");
client.Authority = url;
RestRequest restRequest = new RestRequest();
CookieContainer cookieContainer = null;
if (IsolatedStorageSettings.ApplicationSettings.Contains("cookie"))
{
cookieContainer = IsolatedStorageSettings.ApplicationSettings["cookieContainer"] as CookieContainer;
Cookie cookie = IsolatedStorageSettings.ApplicationSettings["cookie"] as Cookie;
if (cookieContainer.Count == 0 && cookie != null)
{
cookieContainer.SetCookies(new Uri(Constant.ROOTURL), string.Format("{0}={1}", cookie.Name, cookie.Value));
}
}
else
{
cookieContainer = new CookieContainer();
}
restRequest.CookieContainer = cookieContainer;
client.BeginRequest(restRequest, (request, response, userState1) =>
{
cookieContainer = response.CookieContainer;
CookieCollection cookies = cookieContainer.GetCookies(new Uri(Constant.ROOTURL));
try
{
IsolatedStorageSettings.ApplicationSettings["cookie"] = cookies["cooper"];
IsolatedStorageSettings.ApplicationSettings["cookieContainer"] = cookieContainer;
IsolatedStorageSettings.ApplicationSettings.Save();
}
catch
{
}
if (response != null)
Deployment.Current.Dispatcher.BeginInvoke(action, response, userState1);
else
Deployment.Current.Dispatcher.BeginInvoke(failed, new Exception("response返回为空!"));
}, userState);
}
示例5: Can_request_get_with_header_on_client
public void Can_request_get_with_header_on_client()
{
var client = new RestClient
{
Authority = "https://api.twitter.com",
UserAgent = "Hammock",
Path = "statuses/public_timeline.json",
DecompressionMethods = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
client.AddHeader("Accept", "application/json");
var response = client.Request();
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
示例6: WowClient
public WowClient(string baseUrl, IAuthenticator authenticator, int apiVersion, string application)
{
Client = new RestClient()
{
BaseUrl = baseUrl,
UserAgent = "wow.ApiLibrary/" + application
};
Client.JsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
{
Error = (sender, e) =>
{
Debug.WriteLine(e.ToString());
},
ConstructorHandling = Newtonsoft.Json.ConstructorHandling.AllowNonPublicDefaultConstructor
};
Client.AddHeader("api-version", apiVersion.ToString());
authenticator.SetAuthentication(Client);
}
示例7: postMessageToTwitter
private void postMessageToTwitter()
{
var credentials = new OAuthCredentials
{
Type = OAuthType.ProtectedResource,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
ConsumerKey = AppSettings.TwitterConsumerKey,
ConsumerSecret = AppSettings.TwitterConsumerKeySecret,
Token = this.accessToken,
TokenSecret = this.accessTokenSecret,
Version = "1.0"
};
var restClient = new RestClient
{
Authority = AppSettings.TwitterStatusUpdateUrl,
HasElevatedPermissions = true,
Credentials = credentials,
Method = WebMethod.Post
};
restClient.AddHeader("Content-Type", "application/x-www-form-urlencoded");
var restRequest = new RestRequest
{
Path = "1/statuses/update.xml?status=" + this.postMessage
};
var ByteData = Encoding.UTF8.GetBytes(this.postMessage);
restRequest.AddPostContent(ByteData);
restClient.BeginRequest(restRequest, new RestCallback(postFinished));
}
示例8: SetAuthentication
/// <summary>
/// Sets the authentication.
/// </summary>
/// <param name="client">The client.</param>
public void SetAuthentication(RestClient client)
{
client.AddHeader("_username", _username);
client.AddHeader("_password", _password);
}
示例9: Tweet
public static void Tweet(string tweet)
{
var credentials = new OAuthCredentials
{
Type = OAuthType.ProtectedResource,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
ConsumerKey = TwitterSettings.ConsumerKey,
ConsumerSecret = TwitterSettings.ConsumerKeySecret,
Token = IsolatedStorageSettings.ApplicationSettings[TWITTER_ACCESS_TOKEN_KEY].ToString(), //The request Token
TokenSecret = IsolatedStorageSettings.ApplicationSettings[TWITTER_ACCESS_SECRET_KEY].ToString(),
Version = "1.0"
};
var restClient = new RestClient
{
Authority = TwitterSettings.StatusUpdateUrl,
HasElevatedPermissions = true,
Credentials = credentials,
Method = WebMethod.Post
};
restClient.AddHeader("Content-Type", "application/x-www-form-urlencoded");
// Create a Rest Request and fire it
var restRequest = new RestRequest
{
Path = "1/statuses/update.xml?status=" + tweet
};
var ByteData = Encoding.UTF8.GetBytes(tweet);
restRequest.AddPostContent(ByteData);
restClient.BeginRequest(restRequest, new RestCallback(TweetCallback));
}