Skip to main content
 首页 » 编程设计

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

2024年02月25日35sharpest

每当从列出的类中运行下面的行时,我在版本 2017.2.0f3 和 2017.2.1f1 中遇到此错误,但它在 5.5.0.f3 中完美运行

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

错误:

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

有没有其他人以前遇到过这个错误并且能够修复它?

请您参考如下方法:

问题是您未能指定 URI 方案(如 http 或 https)。

因此你必须改变:

127.0.0.1:8888 

到:

http://127.0.0.1:8888