public static async Task <PostStream> GetActivities(string userID, string circleID, string squareID, string categoryID) { PostStream posts = new PostStream(); posts.posts = new ObservableCollection <Post>(); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = System.Net.Http.Headers.AuthenticationHeaderValue.Parse("Bearer " + await oAuth.GetAccessToken()); //client.DefaultRequestHeaders.Add("User-Agent", "com.google.android.apps.plus/411514804 (Linux; U; Android 4.1.2; pl_PL; IdeaTabA1000L-F; Build/JZO54K); G+ SDK"); string postData = returnRequestPost(userID, circleID, squareID, categoryID); HttpContent content = new StringContent(postData); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.PostAsync(new Uri(ACTIVITIES_API), content); //Debug.WriteLine(await response.Content.ReadAsStringAsync()); if (response.IsSuccessStatusCode == true) { JObject result = JObject.Parse(await response.Content.ReadAsStringAsync()); if (result["stream"]["update"] != null) { foreach (var i in result["stream"]["update"]) { posts.posts.Add(_parsePost(i)); } posts.pageToken = (string)result["stream"]["continuationToken"]; } } return(posts); }
public static async Task <PostStream> QueryPost(string queryText, string sort) { PostStream posts = new PostStream(); posts.posts = new ObservableCollection <Post>(); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = System.Net.Http.Headers.AuthenticationHeaderValue.Parse("Bearer " + await oAuth.GetAccessToken()); //client.DefaultRequestHeaders.Add("User-Agent", "com.google.android.apps.plus/411514804 (Linux; U; Android 4.1.2; pl_PL; IdeaTabA1000L-F; Build/JZO54K); G+ SDK"); JObject json = new JObject( new JProperty("activityRequestData", new JObject( new JProperty("activityFilters", new JObject( new JProperty("maxComments", 0) // new JProperty("updateFilter", new JObject( // new JProperty("includeNamespace", new JArray( // "STREAM", // "EVENT", // "SEARCH", // "PLUSONE", // "PHOTO", // "A2A", // "BIRTHDAY", // "PHOTOS_ADDED_TO_EVENT" //] // ) )))), new JProperty("searchQuery", new JObject( new JProperty("sort", sort), new JProperty("queryText", queryText), new JProperty("filter", "TACOS") )) ); HttpContent content = new StringContent(json.ToString()); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.PostAsync(new Uri(SEARCH_API), content); Debug.WriteLine(await response.Content.ReadAsStringAsync()); if (response.IsSuccessStatusCode == true) { JObject result = JObject.Parse(await response.Content.ReadAsStringAsync()); foreach (var i in result["results"]["activityResults"]["stream"]["update"]) { posts.posts.Add(_parsePost(i)); } posts.pageToken = (string)result["results"]["activityResults"]["stream"]["continuationToken"]; return(posts); } else { return(null); } }
IEnumerator UploadScore(string name, string score) { PostStream poststream = new PostStream(); poststream.BeginWrite(true); poststream.Write("username", name); poststream.Write("score", score); poststream.EndWrite(); WWW www = new WWW("www.sussex.ac.uk/Users/wcwc20/Project/UploadScore.php", poststream.BYTES, poststream.headers); yield return(www); if (www.error != null) { Debug.LogError(www.error); } }
IEnumerator DownloadScores(string name, string score) { WWW www = new WWW("www.sussex.ac.uk/Users/wcwc20/Project/DownloadScores.php"); yield return(www); if (www.error != null) { Debug.LogError(www.error); } else { int count = 0; PostStream poststream = new PostStream(); poststream.BeginRead(www, true); poststream.ReadInt(ref count); if (count > 0) { m_hiscores = new string[count]; } for (int i = 0; i < count; i++) { string tname = ""; string tscore = ""; poststream.ReadString(ref tname); poststream.ReadString(ref tscore); m_hiscores[i] = tname + ":" + tscore; } bool ok = poststream.EndRead(); if (!ok) { Debug.LogError("MD5 error"); } } }
private HttpWebRequest CreateRequest() { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.AllowAutoRedirect = false; req.CookieContainer = new CookieContainer(); req.Headers.Add("Accept-Language", defaultLanguage); req.Accept = accept; req.UserAgent = userAgent; req.KeepAlive = false; foreach (string key in ExHeader.Keys) { req.Headers.Add(key, ExHeader[key]); } if (context.Cookies != null) { req.CookieContainer.Add(context.Cookies); } if (!string.IsNullOrEmpty(context.Referer)) { req.Referer = context.Referer; } if (verb == HttpVerb.HEAD) { req.Method = "HEAD"; return(req); } if (postingData.Count > 0 || files.Count > 0 || PostStream != null) { verb = HttpVerb.POST; } if (verb == HttpVerb.POST) { req.Method = "POST"; if (PostStream != null) { Stream requestStream = req.GetRequestStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = PostStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } requestStream.Close(); PostStream.Close(); } else { MemoryStream memoryStream = new MemoryStream(); StreamWriter writer = new StreamWriter(memoryStream); if (files.Count > 0) { string newLine = "\r\n"; string boundary = Guid.NewGuid().ToString().Replace("-", ""); req.ContentType = "multipart/form-data; boundary=" + boundary; foreach (string key in postingData.Keys) { writer.Write("--" + boundary + newLine); writer.Write("Content-Disposition: form-data; name=\"{0}\"{1}{1}", key, newLine); writer.Write(postingData[key] + newLine); } foreach (HttpUploadingFile file in files) { writer.Write("--" + boundary + newLine); writer.Write( "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.FieldName, file.FileName, newLine ); writer.Write("Content-Type: application/octet-stream" + newLine + newLine); writer.Flush(); memoryStream.Write(file.Data, 0, file.Data.Length); writer.Write(newLine); writer.Write("--" + boundary + newLine); } } else { req.ContentType = "application/x-www-form-urlencoded"; StringBuilder sb = new StringBuilder(); foreach (string key in postingData.Keys) { sb.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(postingData[key])); } if (sb.Length > 0) { sb.Length--; } writer.Write(sb.ToString()); } writer.Flush(); using (Stream stream = req.GetRequestStream()) { memoryStream.WriteTo(stream); } } } if (startPoint != 0 && endPoint != 0) { req.AddRange(startPoint, endPoint); } else if (startPoint != 0 && endPoint == 0) { req.AddRange(startPoint); } Reset(); return(req); }