From 6dff9779a349945d502dee67d5d4dd8e63b4f753 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 25 Sep 2017 17:39:51 -0400 Subject: use POST for SMAPI update checks to avoid issues with long queries (#336) --- src/StardewModdingAPI/Framework/WebApiClient.cs | 29 ++++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'src/StardewModdingAPI/Framework') diff --git a/src/StardewModdingAPI/Framework/WebApiClient.cs b/src/StardewModdingAPI/Framework/WebApiClient.cs index 0ee57648..8f0b403d 100644 --- a/src/StardewModdingAPI/Framework/WebApiClient.cs +++ b/src/StardewModdingAPI/Framework/WebApiClient.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Net; +using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using StardewModdingAPI.Models; @@ -40,8 +41,10 @@ namespace StardewModdingAPI.Framework /// The mod keys for which to fetch the latest version. public async Task> GetModInfoAsync(params string[] modKeys) { - string url = $"v{this.Version}/mods?modKeys={Uri.EscapeDataString(string.Join(",", modKeys))}"; - return await this.GetAsync>(url); + return await this.PostAsync>( + $"v{this.Version}/mods", + new ModSearchModel(modKeys) + ); } @@ -49,13 +52,27 @@ namespace StardewModdingAPI.Framework ** Private methods *********/ /// Fetch the response from the backend API. - /// The expected response type. + /// The body content type. + /// The expected response type. /// The request URL, optionally excluding the base URL. - private async Task GetAsync(string url) + /// The body content to post. + private async Task PostAsync(string url, TBody content) { - // build request (avoid HttpClient for Mac compatibility) + /*** + ** Note: avoid HttpClient for Mac compatibility. + ***/ + + // serialise content + byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)); + + // build request HttpWebRequest request = WebRequest.CreateHttp(new Uri(this.BaseUrl, url).ToString()); + request.Method = "POST"; request.UserAgent = $"SMAPI/{this.Version}"; + request.ContentType = "application/json"; + request.ContentLength = data.Length; + using (Stream bodyStream = request.GetRequestStream()) + bodyStream.Write(data, 0, data.Length); // fetch data using (WebResponse response = await request.GetResponseAsync()) @@ -63,7 +80,7 @@ namespace StardewModdingAPI.Framework using (StreamReader reader = new StreamReader(responseStream)) { string responseText = reader.ReadToEnd(); - return JsonConvert.DeserializeObject(responseText); + return JsonConvert.DeserializeObject(responseText); } } } -- cgit