public static Schema FetchSchema(string apiKey) { var url = "http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=" + apiKey; string cachefile = "tf_schema.cache"; string result; HttpWebResponse response = SteamWeb.Request(url, "GET"); DateTime SchemaLastModified = DateTime.Parse(response.Headers["Last-Modified"]); if (!System.IO.File.Exists(cachefile) || (SchemaLastModified > System.IO.File.GetCreationTime(cachefile))) { StreamReader reader = new StreamReader(response.GetResponseStream()); result = reader.ReadToEnd(); File.WriteAllText(cachefile, result); System.IO.File.SetCreationTime(cachefile, SchemaLastModified); } else { TextReader reader = new StreamReader(cachefile); result = reader.ReadToEnd(); reader.Close(); } response.Close(); SchemaResult schemaResult = JsonConvert.DeserializeObject <SchemaResult> (result); return(schemaResult.result ?? null); }
/// <summary> /// Fetches the Tf2 Item schema. /// </summary> /// <param name="apiKey">The API key.</param> /// <returns>A deserialized instance of the Item Schema.</returns> /// <remarks> /// The schema will be cached for future use if it is updated. /// </remarks> public static TF2Schema FetchSchema(string apiKey) { var url = SchemaApiUrlBase + apiKey; // just let one thread/proc do the initial check/possible update. bool wasCreated; var mre = new EventWaitHandle(false, EventResetMode.ManualReset, SchemaMutexName, out wasCreated); // the thread that create the wait handle will be the one to // write the cache file. The others will wait patiently. if (!wasCreated) { bool signaled = mre.WaitOne(10000); if (!signaled) { return(null); } } HttpWebResponse response = SteamWeb.Request(url, "GET"); DateTime schemaLastModified = response.LastModified; string result = GetSchemaString(response, schemaLastModified); response.Close(); mre.Set(); SchemaResult schemaResult = JsonConvert.DeserializeObject <SchemaResult> (result); return(schemaResult.result ?? null); }
public static Schema FetchSchema(string apiKey) { var url = "http://api.steampowered.com/IEconItems_570/GetSchema/v0001/?key=" + apiKey + "&language=en"; string cachefile = "d2_schema.cache"; string result = ""; try { HttpWebResponse response = SteamWeb.Request(url, "GET"); DateTime SchemaLastModified = DateTime.Now; try { SchemaLastModified = DateTime.Parse(response.Headers["Last-Modified"]); } catch { SchemaLastModified = DateTime.Now; } if (!System.IO.File.Exists(cachefile) || (SchemaLastModified > System.IO.File.GetCreationTime(cachefile))) { StreamReader reader = new StreamReader(response.GetResponseStream()); result = reader.ReadToEnd(); File.WriteAllText(cachefile, result); System.IO.File.SetCreationTime(cachefile, SchemaLastModified); } else { TextReader reader = new StreamReader(cachefile); result = reader.ReadToEnd(); reader.Close(); } response.Close(); } catch (NullReferenceException ex) { //.net 4.5 will error out on Request. using (var wc = new WebClient()) { result = wc.DownloadString(url); } } SchemaResult schemaResult = JsonConvert.DeserializeObject <SchemaResult> (result); return(schemaResult.result ?? null); }
private static string RetryWebRequest(string url, string method, NameValueCollection data, CookieContainer cookies, bool ajax = false, string referer = "") { for (int i = 0; i < 10; i++) { try { var response = SteamWeb.Request(url, method, data, cookies, ajax, referer); using (System.IO.Stream responseStream = response.GetResponseStream()) { using (var reader = new System.IO.StreamReader(responseStream)) { string result = reader.ReadToEnd(); if (string.IsNullOrEmpty(result)) { Console.WriteLine("Web request failed (status: {0}). Retrying...", response.StatusCode); System.Threading.Thread.Sleep(1000); } else { return(result); } } } } catch (WebException ex) { try { if (ex.Status == WebExceptionStatus.ProtocolError) { Console.WriteLine("Status Code: {0}, {1}", (int)((HttpWebResponse)ex.Response).StatusCode, ((HttpWebResponse)ex.Response).StatusDescription); } Console.WriteLine("Error: {0}", new System.IO.StreamReader(ex.Response.GetResponseStream()).ReadToEnd()); } catch { } } catch (Exception ex) { Console.WriteLine(ex); } } return(""); }
/// <summary> /// Send the current trade offer. /// </summary> /// <param name="message">Message to send with trade offer.</param> /// <param name="token">Optional trade offer token.</param> /// <returns>-1 if response fails to deserialize (general error), 0 if no tradeofferid exists (Steam error), or the Trade Offer ID of the newly created trade offer.</returns> public ulong SendTrade(string message, string token = "") { var url = "https://steamcommunity.com/tradeoffer/new/send"; var referer = "http://steamcommunity.com/tradeoffer/new/?partner=" + partnerId.AccountID; var data = new NameValueCollection(); data.Add("sessionid", sessionId); data.Add("partner", partnerId.ConvertToUInt64().ToString()); data.Add("tradeoffermessage", message); data.Add("json_tradeoffer", JsonConvert.SerializeObject(this.tradeStatus)); data.Add("trade_offer_create_params", token == "" ? "{}" : "{\"trade_offer_access_token\":\"" + token + "\"}"); try { string result = ""; for (int i = 0; i < 10; i++) { try { var response = SteamWeb.Request(url, "POST", data, cookies, true, referer); using (System.IO.Stream responseStream = response.GetResponseStream()) { using (var reader = new System.IO.StreamReader(responseStream)) { result = reader.ReadToEnd(); if (string.IsNullOrEmpty(result)) { Console.WriteLine("Web request failed (status: {0}). Retrying...", response.StatusCode); System.Threading.Thread.Sleep(1000); } else { break; } } } } catch (WebException ex) { try { int statusCode = 0; if (ex.Status == WebExceptionStatus.ProtocolError) { statusCode = (int)((HttpWebResponse)ex.Response).StatusCode; Console.WriteLine("Status Code: {0}, {1}", statusCode, ((HttpWebResponse)ex.Response).StatusDescription); } string errorMessage = new System.IO.StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); Console.WriteLine("Error: {0}", errorMessage); if (statusCode == 500 && errorMessage.Contains("There was an error sending your trade offer.")) { var errorJson = JsonConvert.DeserializeObject <dynamic>(errorMessage); if (errorJson.strError != null) { string errorString = errorJson.strError; int errorCode = Convert.ToInt32(errorString.Split(new char[] { '(', ')' })[1]); if (errorCode == 16 || errorCode == 11) { Console.WriteLine("Encountered Steam error code {0}, manually checking for completion...", errorCode); var tradeOfferList = tradeOffers.GetTradeOffers(); foreach (var tradeOffer in tradeOfferList) { if (tradeStatus.me.assets.Count == tradeOffer.ItemsToGive.Length && tradeStatus.them.assets.Count == tradeOffer.ItemsToReceive.Length) { foreach (var item in tradeOffer.ItemsToGive) { var asset = new TradeAsset(item.AppId, Convert.ToInt64(item.ContextId), item.AssetId.ToString(), item.Amount); if (!tradeStatus.me.assets.Contains(asset)) { Console.WriteLine("Could not validate that this trade offer was sent successfully. (1)"); return(0); } } foreach (var item in tradeOffer.ItemsToReceive) { var asset = new TradeAsset(item.AppId, Convert.ToInt64(item.ContextId), item.AssetId.ToString(), item.Amount); if (!tradeStatus.them.assets.Contains(asset)) { Console.WriteLine("Could not validate that this trade offer was sent successfully. (2)"); return(0); } } Console.WriteLine("Successfully validated!"); return(tradeOffer.Id); } } } else if (errorCode == 15) { throw new TradeOfferException(errorString, errorCode); } } } } catch { } } catch (Exception ex) { Console.WriteLine(ex); } } var jsonResponse = JsonConvert.DeserializeObject <dynamic>(result); try { return(Convert.ToUInt64(jsonResponse.tradeofferid)); } catch { return(0); } } catch { return(0); } }