Advertisement
Guest User

Untitled

a guest
Jun 29th, 2014
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 KB | None | 0 0
  1. -- MAIN --
  2.  
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace GW2TPv2
  12. {
  13.     class Program
  14.     {
  15.         public static void Main(string[] args)
  16.         {
  17.  
  18.             GW2TP c = new GW2TP();
  19.             Console.WriteLine(c.getBasket());
  20.             c.connectToTp("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
  21.             Console.WriteLine(c.getBasket());
  22.             c.queryListing(19697);
  23.             Console.WriteLine(c.getBasket());
  24.  
  25.             string test = c.getBasket();
  26.  
  27.             // Order o = JsonConvert.DeserializeObject<Order>(test);
  28.  
  29.             Console.ReadLine();
  30.         }
  31.     }
  32. }
  33.  
  34.  
  35.  
  36.  
  37. -- GW2TP --
  38. using System.Runtime.Serialization;
  39. using System;
  40. using System.Collections.Generic;
  41. using System.Linq;
  42. using System.Net;
  43. using System.Net.Http;
  44. using System.Text;
  45. using System.Threading.Tasks;
  46.  
  47. namespace GW2TPv2
  48. {
  49.     class GW2TP
  50.     {
  51.         private HttpClient httpClient;                 // Does HTTP requests
  52.         private HttpClientHandler httpClientHandler;   // Sets optional parameters
  53.         private CookieContainer cookieContainer;       // Holds cookies we need to send
  54.         private Uri baseAddress;                       // Url of the TP
  55.         private Uri itemListingsAddress;               // Url to get in-game item listings
  56.         //private string sessionId;                    // Our session id from in-game
  57.         private string basket;                         // Temporarily holds data
  58.  
  59.         public GW2TP()
  60.         {
  61.             setup();
  62.         }
  63.  
  64.         ~GW2TP()
  65.         {
  66.             httpClient.Dispose();
  67.         }
  68.  
  69.         public string getBasket()
  70.         {
  71.             return basket;
  72.         }
  73.  
  74.         private void setup()
  75.         {
  76.             // Initial setup
  77.             setAddresses();
  78.             createCookieContainer();
  79.             createHttpClientHandler();
  80.  
  81.             basket = "pie";
  82.         }
  83.  
  84.         private void setAddresses()
  85.         {
  86.             // Our base url address is the TP
  87.             baseAddress = new Uri("http://tradingpost-dfw-live.ncplatform.net");
  88.             itemListingsAddress = new Uri("https://tradingpost-dfw-live.ncplatform.net/ws/listings.json?id=");
  89.         }
  90.  
  91.         private void createCookieContainer()
  92.         {
  93.             // Create our cookie container that will
  94.             // hold cookies we set (namely the session id)
  95.             cookieContainer = new CookieContainer();
  96.         }
  97.  
  98.         private void createHttpClientHandler()
  99.         {
  100.             // Create the custom http handler so we can
  101.             // bind this to our custom cookie container
  102.             // and use custom cookies in http requests
  103.             // made by HttpClient
  104.             httpClientHandler = new HttpClientHandler()
  105.             {
  106.                 CookieContainer = cookieContainer,
  107.                 UseCookies = true
  108.             };
  109.         }
  110.  
  111.         private void createHttpClient()
  112.         {
  113.             // Instantiate a HttpClient with our
  114.             // custom HttpClientHandler
  115.             httpClient = new HttpClient(httpClientHandler)
  116.             {
  117.                 BaseAddress = baseAddress
  118.             };
  119.  
  120.             // Add other custom headers if necessary
  121.             httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
  122.  
  123.             httpClient.MaxResponseContentBufferSize = 256000;
  124.         }
  125.  
  126.         private void addCookie(string cookieName, string cookieValue)
  127.         {
  128.             // Add a cookie to our http request
  129.             cookieContainer.Add(baseAddress, new Cookie(cookieName, cookieValue));
  130.         }
  131.  
  132.         public async void connectToTp(string sessionId)
  133.         {
  134.             // Create a HttpClient if one isn't
  135.             // already instantiated
  136.             if (httpClient == null)
  137.             {
  138.                 createHttpClient();
  139.             }
  140.  
  141.             // Add our session id to the http request
  142.             addCookie("s", sessionId);
  143.  
  144.             // Connect to the TP
  145.             basket = await query(baseAddress.ToString());
  146.         }
  147.  
  148.         private async Task<string> query(string url)
  149.         {
  150.             // This can throw exceptions, warning!
  151.             try
  152.             {
  153.                 // Send an asyncronous GET reqest
  154.                 HttpResponseMessage httpResponse = await httpClient.GetAsync(url);
  155.                 httpResponse.EnsureSuccessStatusCode();
  156.  
  157.                 // The content of the return message is
  158.                 // saved in "basket"
  159.                 basket = await httpResponse.Content.ReadAsStringAsync();
  160.                 return basket;
  161.             }
  162.             catch (HttpRequestException hre)
  163.             {
  164.                 Console.WriteLine("HttpRequestException:");
  165.                 Console.WriteLine(hre.ToString());
  166.             }
  167.             catch (Exception ex)
  168.             {
  169.                 Console.WriteLine("Exception:");
  170.                 Console.WriteLine(ex.ToString());
  171.             }
  172.  
  173.             return "";
  174.         }
  175.  
  176.         public async void queryListing(int itemId, bool buys = true, bool sells = true)
  177.         {
  178.             // Create our url to query the item listings
  179.             string url = itemListingsAddress.ToString() + itemId.ToString();
  180.  
  181.             // Append additional parameters if we don't want
  182.             // buy or sell orders
  183.             url = (!buys) ? url + "&buys=false" : url;
  184.             url = (!sells) ? url + "&sells=false" : url;
  185.  
  186.             basket = await query(url);
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement