Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MAIN --
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- namespace GW2TPv2
- {
- class Program
- {
- public static void Main(string[] args)
- {
- GW2TP c = new GW2TP();
- Console.WriteLine(c.getBasket());
- c.connectToTp("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
- Console.WriteLine(c.getBasket());
- c.queryListing(19697);
- Console.WriteLine(c.getBasket());
- string test = c.getBasket();
- // Order o = JsonConvert.DeserializeObject<Order>(test);
- Console.ReadLine();
- }
- }
- }
- -- GW2TP --
- using System.Runtime.Serialization;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- namespace GW2TPv2
- {
- class GW2TP
- {
- private HttpClient httpClient; // Does HTTP requests
- private HttpClientHandler httpClientHandler; // Sets optional parameters
- private CookieContainer cookieContainer; // Holds cookies we need to send
- private Uri baseAddress; // Url of the TP
- private Uri itemListingsAddress; // Url to get in-game item listings
- //private string sessionId; // Our session id from in-game
- private string basket; // Temporarily holds data
- public GW2TP()
- {
- setup();
- }
- ~GW2TP()
- {
- httpClient.Dispose();
- }
- public string getBasket()
- {
- return basket;
- }
- private void setup()
- {
- // Initial setup
- setAddresses();
- createCookieContainer();
- createHttpClientHandler();
- basket = "pie";
- }
- private void setAddresses()
- {
- // Our base url address is the TP
- baseAddress = new Uri("http://tradingpost-dfw-live.ncplatform.net");
- itemListingsAddress = new Uri("https://tradingpost-dfw-live.ncplatform.net/ws/listings.json?id=");
- }
- private void createCookieContainer()
- {
- // Create our cookie container that will
- // hold cookies we set (namely the session id)
- cookieContainer = new CookieContainer();
- }
- private void createHttpClientHandler()
- {
- // Create the custom http handler so we can
- // bind this to our custom cookie container
- // and use custom cookies in http requests
- // made by HttpClient
- httpClientHandler = new HttpClientHandler()
- {
- CookieContainer = cookieContainer,
- UseCookies = true
- };
- }
- private void createHttpClient()
- {
- // Instantiate a HttpClient with our
- // custom HttpClientHandler
- httpClient = new HttpClient(httpClientHandler)
- {
- BaseAddress = baseAddress
- };
- // Add other custom headers if necessary
- httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
- httpClient.MaxResponseContentBufferSize = 256000;
- }
- private void addCookie(string cookieName, string cookieValue)
- {
- // Add a cookie to our http request
- cookieContainer.Add(baseAddress, new Cookie(cookieName, cookieValue));
- }
- public async void connectToTp(string sessionId)
- {
- // Create a HttpClient if one isn't
- // already instantiated
- if (httpClient == null)
- {
- createHttpClient();
- }
- // Add our session id to the http request
- addCookie("s", sessionId);
- // Connect to the TP
- basket = await query(baseAddress.ToString());
- }
- private async Task<string> query(string url)
- {
- // This can throw exceptions, warning!
- try
- {
- // Send an asyncronous GET reqest
- HttpResponseMessage httpResponse = await httpClient.GetAsync(url);
- httpResponse.EnsureSuccessStatusCode();
- // The content of the return message is
- // saved in "basket"
- basket = await httpResponse.Content.ReadAsStringAsync();
- return basket;
- }
- catch (HttpRequestException hre)
- {
- Console.WriteLine("HttpRequestException:");
- Console.WriteLine(hre.ToString());
- }
- catch (Exception ex)
- {
- Console.WriteLine("Exception:");
- Console.WriteLine(ex.ToString());
- }
- return "";
- }
- public async void queryListing(int itemId, bool buys = true, bool sells = true)
- {
- // Create our url to query the item listings
- string url = itemListingsAddress.ToString() + itemId.ToString();
- // Append additional parameters if we don't want
- // buy or sell orders
- url = (!buys) ? url + "&buys=false" : url;
- url = (!sells) ? url + "&sells=false" : url;
- basket = await query(url);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement