之前分别给出了人人网在WP8.1RT和SL下的调用示例.新浪微博方法也基本一样.小梦今天给出大家在WP8.1RT中调用新浪微博API的具体源码.包含:
大家有兴趣的话可以依次为例去做一个新浪微博客户端.下面我直接上代码:
private void WeiBoNavigate()//导航至微博登录 { web.Navigate(new Uri(weiboUri, UriKind.Absolute)); web.NavigationStarting += web_NavigationStarting; } void web_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) { if (args.Uri.AbsoluteUri.ToString().Contains("code")) { codeString = args.Uri.AbsoluteUri.ToString(); getCode(codeString); } } private void getCode(string codeString) { int i = 0, j = 0; i = codeString.IndexOf("code"); j = codeString.Length - i - 5; string code= codeString.Substring(i + 5, j); HttpWeiboPost.HttpPost(key, secret, redirect_uri, code); Frame.Navigate(typeof(MainPage)); }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Data.Json; using Windows.UI.Popups; using Windows.Web.Http; namespace WeiBo { public class HttpWeiboPost { public static async void HttpPost(string client_id, string client_secret, string redirect_uri, string code) { try { HttpClient httpClient = new HttpClient(); string posturi = "https://api.weibo.com/oauth2/access_token?"; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(posturi)); HttpFormUrlEncodedContent postData = new HttpFormUrlEncodedContent( new List> { new KeyValuePair ("client_id", client_id), new KeyValuePair ("client_secret", client_secret), new KeyValuePair ("grant_type","authorization_code"), new KeyValuePair ("redirect_uri", redirect_uri), new KeyValuePair ("code",code), } ); request.Content = postData; HttpResponseMessage response = await httpClient.SendRequestAsync(request); string responseString = await response.Content.ReadAsStringAsync(); JsonObject token = JsonObject.Parse(responseString); string access_token = token.GetNamedString("access_token").ToString(); App.Access_token=access_token; App.appSettings.Values["access_token"] = access_token; } catch (Exception ex) { Msg(ex.Message.ToString()); } } private static async void Msg(string msg) { await new MessageDialog(msg).ShowAsync(); } } }
public class HttpWeiboGet { private static string responseString; public static async void HttpGet(string uri) { HttpClient httpClient = new HttpClient(); //var headers = httpClient.DefaultRequestHeaders; //headers.UserAgent.ParseAdd("ie"); //headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); try { HttpResponseMessage response = new HttpResponseMessage(); response = await httpClient.GetAsync(new Uri(uri, UriKind.Absolute)); response.EnsureSuccessStatusCode(); responseString = await response.Content.ReadAsStringAsync(); JsonObject token = JsonObject.Parse(responseString); string UID = token.GetNamedNumber("uid").ToString(); App.appSettings.Values["userID"] = UID; } catch (Exception ex) { Msg("网络请求失败!" + ex.Message.ToString()); } } private static async void Msg(string msg) { await new MessageDialog(msg).ShowAsync(); } }
public class HttpWeiboGetUseInfo { private static string responseString; public static async void HttpGet(string uri) { HttpClient httpClient = new HttpClient(); //var headers = httpClient.DefaultRequestHeaders; //headers.UserAgent.ParseAdd("ie"); //headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); try { HttpResponseMessage response = new HttpResponseMessage(); response = await httpClient.GetAsync(new Uri(uri, UriKind.Absolute)); response.EnsureSuccessStatusCode(); responseString = await response.Content.ReadAsStringAsync(); JsonObject user = JsonObject.Parse(responseString); string UID = user.GetNamedNumber("id").ToString(); string name = user.GetNamedString("name").ToString(); string description = user.GetNamedString("description").ToString(); } catch (Exception ex) { Msg("网络请求失败!" + ex.Message.ToString()); } } private static async void Msg(string msg) { await new MessageDialog(msg).ShowAsync(); } }
public class HttpWeiboPostNew { public static async void HttpPost(string postMsg) { try { HttpClient httpClient = new HttpClient(); string posturi = "https://api.weibo.com/2/statuses/update.json?"; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(posturi)); HttpFormUrlEncodedContent postData = new HttpFormUrlEncodedContent( new List> { new KeyValuePair ("access_token",App.Access_token), new KeyValuePair ("status", postMsg), } ); request.Content = postData; HttpResponseMessage response = await httpClient.SendRequestAsync(request); string responseString = await response.Content.ReadAsStringAsync(); } catch (Exception ex) { Msg(ex.Message.ToString()); } } private static async void Msg(string msg) { await new MessageDialog(msg).ShowAsync(); } }
public class HttpWeiboImagePost { public static async void HttpImagePost(string status, string pic ) { HttpClient httpClient = new HttpClient(); try { string posturi = "https://upload.api.weibo.com/2/statuses/upload.json?access_token=" + App.Access_token; StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri(pic, UriKind.Absolute)); IRandomAccessStreamWithContentType stream1 = await file1.OpenReadAsync(); HttpStreamContent streamContent1 = new HttpStreamContent(stream1); HttpStringContent stringContent = new HttpStringContent(status); HttpMultipartFormDataContent fileContent = new HttpMultipartFormDataContent(); fileContent.Add(stringContent, "status"); fileContent.Add(streamContent1, "pic", "pic.jpg"); HttpResponseMessage response = await httpClient.PostAsync(new Uri(posturi), fileContent); string responString = await response.Content.ReadAsStringAsync(); } catch (Exception ex) { Msg(ex.Message.ToString()); } } private static async void Msg(string msg) { await new MessageDialog(msg).ShowAsync(); } }