private string webPost1(string _URI, string _postString) { var client = new WebClient(); client.DownloadStringCompleted += (s, ev) => { result = parseToHighscoreTable(ev.Result); }; string str = "[URL to server web service]/Leaderboard/requestscores.php"; UriBuilder uri = new UriBuilder(str); uri.Query = "ModeID=1&Format=TOP10"; client.DownloadStringAsync(uri.Uri); return(responseString); }
private string webPost(string _URI, string _postString) { isResultOK = false; isRequestFinished = false; Stream dataStream = null; StreamReader reader = null; HttpWebResponse response = null; responseString = null; Uri uri = new Uri(_URI); // Create a request using a URL that can receive a post. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); // Set the Method property of the request to POST. request.Method = REQUEST_METHOD_POST; // Create POST data and convert it to a byte array. string postData = _postString; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = CONTENT_TYPE; // Set the ContentLength property of the WebRequest. //request.ContentLength = byteArray.Length; // Get the request stream. dataStream = HttpWebRequestExtensions.GetRequestStream((HttpWebRequest)request); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. var callback = new AsyncCallback(delegate(IAsyncResult asynchronousResult) { try { response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. reader = new StreamReader(dataStream); // Read the content. responseString = reader.ReadToEnd(); // Display the content. //Console.WriteLine(responseFromServer); // Clean up the streams. if (reader != null) { reader.Close(); } if (dataStream != null) { dataStream.Close(); } if (response != null) { response.Close(); } result = parseToHighscoreTable(responseString); isResultOK = true; } catch (Exception ex) { isResultOK = false; } isRequestFinished = true; }); response = HttpWebRequestExtensions.GetResponse((HttpWebRequest)request, callback); return(responseString); }