So the default code on the website example for C# is
using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;
namespace CSHttpClientSample
{
static class Program
{
static void Main()
{
MakeRequest();;
Console.WriteLine("Hit ENTER to exit...");
Console.ReadLine();
}
static async void MakeRequest()
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add("api_key", "{subscription key}");
var uri = "https://api.wmata.com/StationPrediction.svc/GetPrediction/{StationCodes}&" + queryString;
var response = await client.GetAsync(uri);
}
}
}
I’m trying to figure out how I would pass the subscription key using unity. I don’t understand much of anything in this code and I’ve got a small example working where I can read the pixel height of a jpg. Getting this info is the next step and I haven’t a clue where to start.
I cant say man. I can’t find the HTTP equivalent either. Ever since unity moved to a WWW class, I’m dumbfounded. I took a stab using the WWW class examples on Unity’s scripting manual and what I could gather from the web itself. The biggest task is setting up the query/parsing mechanism, and dealing with the header setting/sending with the key/value settings required to receive information from that API. Feel free to use what I got- I had an error with passing the header values/
Any help from the community on converting this code to a unity equivalent??? This would be extremely useful and eye opening. I seem to be at a lost when it comes to network class stuff.
Once you get something working, the data receieved should be in a JSON format which is nothing but a string of data. Unity’s asset store has a “Newtonsoft JSON.net” package to get what you want,doesn’t require DLLs, and there’s also a UnityWiki version called SimpleJSON.cs that does simple grabs.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Net;
using System.Text;
public class GetListOfStationsPerLine : MonoBehaviour {
public string SubscriptionKey = "{MySubCode}"; //API Subkey is kept private for this example
public string StationCode = "{B03}"; //Glenmont Station
public string DCTrainurl = "https://api.wmata.com/StationPrediction.svc/GetPrediction/"+StationCode+"&"; //https://api.wmata.com/StationPrediction.svc/GetPrediction/{StationCodes}&";
void Start(){
StartCoroutine (MakeRequest ());
Console.WriteLine ("Hit Enter to Exit...");
Console.ReadLine ();
}
IEnumerator MakeRequest(){
//Initialize Client End Protcol
//var client = new HttpClient();
//var queryString = HttpUtility.ParseQueryString(string.Empty);
//EQUIVALENT ->
WWWForm Client = new WWWForm ();
int ConvertTrainSubnum = int.Parse (SubscriptionKey);
// Request headers
//client.DefaultRequestHeaders.Add("api_key", "{subscription key}");
//EQUIVALENT ->
Client.AddField("api_key",int.TryParse (SubscriptionKey)); //header value required int equivalent hence the int.parse above
Hashtable headers = Client.headers;
byte[] rawData = Client.data;
// Request parameters
//queryString["StationCodes"] = "{string}";
//string uri = "https://api.wmata.com/StationPrediction.svc/GetPrediction/{StationCodes}&" + queryString;
//CAN'T FIND EQUIVALENT
//Send and Get Response
//var response = await client.GetAsync(uri);
//EQUIVALENT
WWW www = new WWW (DCTrainurl, rawData,headers);
yield return www;
//Show the JSON string recieved
Debug.Log (www.text);
}
}