Unity使用http向服务器请求数据

方便快捷,记录一下!

 void Start() {
    
    
        HttpRequest("我在请求内容","http://localhost:8000//demo//abc");
    }
public void HttpRequest(string postData,string url) {
    
    
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
        webRequest.Method = "POST";            
        webRequest.ContentType = "text/plain";
        webRequest.ContentLength = byteArray.Length;
        Stream newStream = webRequest.GetRequestStream();
        newStream.Write(byteArray, 0, byteArray.Length);
        newStream.Close();
        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        Debug.Log("响应我的消息:"+sr.ReadToEnd());
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33547099/article/details/112257555
今日推荐