JamesVeug
JamesVeug

Reputation: 260

WWW WebRequest UriFormatException: URI scheme must start with a letter and must consist of one of

I've been getting this error in versions 2017.2.0f3 and 2017.2.1f1 whenever running the line below from the class listed, however it works perfectly in 5.5.0.f3

WWW request = new WWW(m_host, bytes, HashtableToDictionary<string, string>(postHeader));

ERROR:

UriFormatException: URI scheme must start with a letter and must consist of one of alphabet, digits, '+', '-' or '.' character.
System.Uri.Parse (UriKind kind, System.String uriString)
System.Uri.ParseUri (UriKind kind)
System.Uri..ctor (System.String uriString, Boolean dontEscape)
System.Uri..ctor (System.String uriString)

Class producing Error:

public class ServerRequest<T> : BaseServerRequest
{
    public string Game;
    public string Content;

    [NonSerialized]
    public bool Successful;
    [NonSerialized]
    public ServerResponseData<T> Response;

    private string m_host;
    private MonoBehaviour owner;

    private bool m_finished = false;
    private bool m_running = false;

    public override object Current
    {
        get
        {
            return null;
        }
    }

    public ServerRequest(Dictionary<string, object> content, string game, string host, MonoBehaviour owner)
    {
        SetContent(content);
        Game = game;
        m_host = host;
        this.owner = owner;
    }

    public IEnumerator Process()
    {
        Debug.Log("Sending Data...");
        UTF8Encoding encoding = new UTF8Encoding();

        string json = TinyJson.JSONParser.ToJson(this);
        Debug.Log("Sending Json...\n" + json);

        byte[] bytes = encoding.GetBytes(json);

        Hashtable postHeader = new Hashtable();
        postHeader.Add("Content-Type", "text/json");
        postHeader.Add("Content-Length", json);


        WWW request = new WWW(m_host, bytes, HashtableToDictionary<string, string>(postHeader));
        yield return request;

        if (request == null)
        {
            Successful = false;
        }
        else if (request.error != null)
        {
            Successful = false;
            Debug.LogError(request.error);
        }
        else
        {
            Successful = true;
            Debug.Log("Response Text: " + request.text);

            string responseJson1 = request.text.ReplaceAll("\\n", "\\\\n").ReplaceAll("\\t", "\\\\t");
            Debug.Log("Response Text2: " + responseJson1);
            Response = TinyJson.JSONParser.FromJson<ServerResponseData<T>>(responseJson1);

            Debug.Log("Response: ");
            Debug.Log("\tContent: " + Response.Content);
            Debug.Log("\tSuccessful: " + Response.Successful);
            Debug.Log("\tMessage: " + Response.Message);
        }

        m_finished = true;
    }

    public static Dictionary<K, V> HashtableToDictionary<K, V>(Hashtable table)
    {
        return table
          .Cast<DictionaryEntry>()
          .ToDictionary(kvp => (K)kvp.Key, kvp => (V)kvp.Value);
    }

    public override string ToString()
    {
        return string.Format("Response: Successfull = {0}, Content = {1}", Successful, Response);
    }

    public override string ToJson()
    {
        return string.Format("{{ \"Game\": \"{0}\", \"Content\": {1} }}", Game, Content);
    }

    public override bool MoveNext()
    {
        if (!m_running)
        {
            m_running = true;
            owner.StartCoroutine(Process());
        }
        return !m_finished;
    }

    public override void Reset()
    {
        // Nope
    }

    public void SetContent(Dictionary<string, object> content)
    {
        Content = TinyJson.JSONParser.ToJson(content);
    }
}

Has anyone else encountered this error before and been able to fix it?

Upvotes: 1

Views: 1507

Answers (1)

mjwills
mjwills

Reputation: 23820

The issue is that you have failed to specify a URI scheme (like http or https).

Thus you must change:

127.0.0.1:8888

to:

http://127.0.0.1:8888

Upvotes: 3

Related Questions