Quantcast
Channel: Questions in topic: "httpwebrequest"
Viewing all articles
Browse latest Browse all 190

HTTP to WWW Help --Small Sample Code Conversion

$
0
0
Hey guys, seems that I have a similar problem as this guy: http://answers.unity3d.com/answers/1088100/view.html I'm new to network programming, and can't for the life of me get this small batch of code to work. I attempted coverting what mojojo provided at the bottom of the page. I got a response, but couldn't get it to properly send back the json information. Here's that example below-feel free to respond on mojojo's question too. Thanks again, Here's DC's metro link with the code sample <- https://developer.wmata.com/docs/services/547636a6f9182302184cda78/operations/547636a6f918230da855363f Here's Metros & Below it was my conversion attempt: **METROS** 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/json/GetPrediction/{StationCodes}&" + queryString; var response = await client.GetAsync(uri); } } } **MY CONVERSION:** using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Net; using System.Text; public class GetListOfStationsPerLineVer3 : MonoBehaviour { public string SubscriptionKey = "ENTER_SUB_KEY_HERE"; //API Subkey is kept private for this example public string StationCode = "B03"; //Glenmont Station void Start(){ StartCoroutine (MakeRequest ()); Console.WriteLine ("Hit Enter to Exit..."); Console.ReadLine (); } IEnumerator MakeRequest(){ //string uri = WWW.EscapeURL ("https://api.wmata.com/StationPrediction.svc/GetPrediction/"+StationCode+"&",System.Text.Encoding.UTF8); string uri = "https://api.wmata.com/StationPrediction.svc/GetPrediction/" + StationCode + "&"; Dictionary headers = new Dictionary (); headers.Add ("api_key", SubscriptionKey); headers ["Authorization"] = "Basic" + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(SubscriptionKey)); WWW wwwRequest = new WWW(uri, null, headers); yield return wwwRequest; if(wwwRequest.error == null) {Debug.Log ("WWW ok!:"+wwwRequest.text); }else{ Debug.Log ("WWW error!:"+wwwRequest.error+"the url is: " +uri); } } }

Viewing all articles
Browse latest Browse all 190

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>