public T Post <T>(IWeiboRequest <T> request) where T : IWeiboResponse, new() { var url = new StringBuilder(request.Url); if (request.WeiboType == WeiboType.Sina) { url.Append(_format); } var form = new HttpUrlEncodedForm(); if (request.WeiboType == WeiboType.QQ) { form.Append("format", _format.ToString()); } if (request.Parameters.Count > 0) { foreach (var parameter in request.Parameters) { form.Append(parameter.Key, parameter.Value); } } var oauthRequest = new OAuthHttpRequestMessage("POST", url.ToString(), form).Sign(_accessToken); var result = _client.Send(oauthRequest).ReadContentAsString(); var response = new T(); response.ConvertFrom(result); return(response); }
/// <summary> /// Authenticate to the named Google/GData service, eg picasa, /// with the given user name. /// </summary> /// <remarks> /// <para> /// If the provided username is blank, then this method will /// prompt the user to provide it, by displaying a form. It /// then will send an authentication message to Google. /// Upon receipt of a successful authentication message, the /// Google authentication token will be stored into this /// (singleton) instance. /// </para> /// </remarks> /// <returns> // The user name (email addy) which is authenticated. /// </returns> public static string Authenticate(string username, string service) { Tracing.Trace("GDataSession::Authenticate user({0}), svc({1})", username, service); var serviceString = LookupService(service); if (GdataSession.GetHeaders(username, service) != null) { return(username); } var info = PromptForAuthInfo(username); if (info == null) { return(null); } var http = new HttpClient(_baseLoginUrl); var form = new HttpUrlEncodedForm(); form.Add("accountType", "GOOGLE"); form.Add("Email", info.email); form.Add("Passwd", info.password); form.Add("service", serviceString); form.Add("source", _appName); var response = http.Post(_relativeLoginUrl, form.CreateHttpContent()); response.EnsureStatusIsSuccessful(); var result = response.Content.ReadAsString(); string auth = RE.Regex.Replace(result, "(?s).*Auth=(.*)", "$1"); // After a successful authentication request, use the Auth // value to create an Authorization header for each request: // // Authorization: GoogleLogin auth=yourAuthValue // // Hereafter, we need to use a HttpClient.Send() overload, in // order to be able to specify headers. instance.SetAuth(info.email, service, auth); Tracing.Trace("GDataSession::Authenticate return '{0}'", info.email); return(info.email); }
public static HttpResponseMessage SendFormData(HttpMethod httpMethod, string restMethod, string[] parameters) { var form = new HttpUrlEncodedForm(); // Create post data from parameters foreach (var p in parameters) { var delim = p.IndexOf("="); var key = p.Substring(0, delim); var value = p.Substring(delim + 1, p.Length - delim - 1); form.Add(key, value); } var content = form.CreateHttpContent(); // Make REST request with parameters return HttpClient.Send(httpMethod, restMethod, content); }
//initiate the request to modify the call to be sent to twilio server with appropriate callback url private static void InitiateCallModify(Entity.CallBase call, string redirectUrl) { if (call == null || string.IsNullOrEmpty(call.CallTwilioId) || string.IsNullOrEmpty(redirectUrl)) { return; } TwilioSettings twilioSettings = new TwilioSettings(); redirectUrl = string.Format("{0}{1}", twilioSettings.TwilioTwiMLCallbackUrl, redirectUrl); var postUrl = string.Format("{0}/Accounts/{1}/Calls/{2}", twilioSettings.TwilioApiVersion, twilioSettings.TwilioAccountSid, call.CallTwilioId); if (log.IsDebugEnabled) { log.Debug("InitiateCallModify.postUrl." + postUrl); } using (HttpClient httpClient = new HttpClient("https://api.twilio.com/")) { HttpUrlEncodedForm frm = new HttpUrlEncodedForm(); //add the form fields that twilio expects frm.Add("Method", "POST"); frm.Add("Url", redirectUrl); //set the security credentials for the http post httpClient.TransportSettings.Credentials = new NetworkCredential(twilioSettings.TwilioAccountSid, twilioSettings.TwilioAuthToken); //send request to twilio using (HttpResponseMessage httpResp = httpClient.Post(postUrl, frm.CreateHttpContent())) { //don't need to do anything with the response of the http post //twilio sends the response in a seperate http request to the callback url } } }
/// <summary> /// Asynchronicznie dodaje status do blipa /// </summary> /// <param name="content">treść</param> public void AddUpdateAsync(string content) { string query = "/updates"; HttpUrlEncodedForm form = new HttpUrlEncodedForm(); form.Add("update[body]", content); //nowy sposób dodawania statusów //blipHttpClient.Post(query, form.CreateHttpContent()); //stary sposób dodawania elementów //blipHttpClient.Post(query,HttpContent.Create(string.Format(@"body={0}",content)) ); lock (httpAsyncClientLock) { //jako state przekazujemy cały obiekt,aby można było pobrać później z niego ResponseMessage blipHttpClientAsync.BeginSend( new HttpRequestMessage("POST", new Uri(query, UriKind.Relative), form.CreateHttpContent()), new AsyncCallback(AfterAddStatusAsync), blipHttpClientAsync); } }
public bool authorize(String authCode) { // get the access token HttpClient http = new HttpClient(baseUri); HttpUrlEncodedForm req = new HttpUrlEncodedForm(); req.Add("code", authCode); req.Add("client_secret", client_secret); req.Add("client_id", client_id); HttpResponseMessage resp = http.Post("access_token", req.CreateHttpContent()); if (resp.StatusCode == System.Net.HttpStatusCode.OK) { token = resp.Content.ReadAsJsonDataContract<AccessToken>(); auth = true; return true; } auth = false; return false; }