WWW WebRequest UriFormatException:URI方案必须以字母开头,并且必须包含一个字母

时间:2017-12-29 09:14:55

标签: c# unity3d web server

我在版本2017.2.0f3和2017.2.1f1中遇到此错误,只要在列出的类中运行以下行,但它在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)

类产生错误:

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);
    }
}

之前是否有其他人遇到此错误并且能够修复它?

1 个答案:

答案 0 :(得分:3)

问题是您未能指定URI方案(例如httphttps)。

因此你必须改变:

127.0.0.1:8888

为:

http://127.0.0.1:8888