Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by OJ3D · Oct 26, 2015 at 09:22 PM · wwwjsonconverthttpwebrequestheaders

HTTP to WWW Help --Small Sample Code Conversion

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<string,string> headers = new Dictionary<string,string> ();
          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);
          }
  
      }
  
  }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JsonUtility returns {} 2 Answers

Error CS1061: 'HttpWebRequest' does not contain a definition for 'ContentLength' 0 Answers

Refresh Chat room messages 1 Answer

Using HTTPS 0 Answers

Deserialize JSON with a certain structure using JsonUtility 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges