WebRequest发送请求并接收返回值

public string getXmlStr(string hphmcode)
        {
            string Url = "http://localhost:8080/readvioinfo.jsp";
            string hphm = hphmcode;
            string hpzl = "02";
            //Encoding encoding = Encoding.GetEncoding("utf-8");
            Encoding encoding = Encoding.GetEncoding("GB2312");
             string postData = "hphm=" + hphm + "&hpzl=" + hpzl;
            byte[] data = encoding.GetBytes(postData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            //发送数据
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
            //接收返回值
            HttpWebResponse res = (HttpWebResponse)request.GetResponse();
            StreamReader sReader = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
            string strBack = sReader.ReadToEnd();
            //Response.Write(strBack);
            sReader.Close();
            res.Close();
            return strBack;
        }

转载于:https://www.cnblogs.com/apes-monkeys/p/4031213.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UnityWebRequest 是 Unity 引擎提供的一种用于发送网络请求的类,它支持多种协议,包括 HTTP、HTTPS、FTP、File 等。下面是一个简单的例子,演示如何使用 UnityWebRequest 发送一个 GET 请求: ```csharp IEnumerator GetRequest(string url) { using (UnityWebRequest webRequest = UnityWebRequest.Get(url)) { yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError("Error: " + webRequest.error); } else { Debug.Log("Response: " + webRequest.downloadHandler.text); } } } ``` 这里使用了 IEnumerator 和 yield 关键字,因为 UnityWebRequest 是异步执行的,需要使用协程来处理。 使用 UnityWebRequest 发送 POST 请求也很简单,只需要将上面的 GetRequest 方法修改为: ```csharp IEnumerator PostRequest(string url, string data) { byte[] bodyRaw = Encoding.UTF8.GetBytes(data); using (UnityWebRequest webRequest = UnityWebRequest.Post(url, "POST")) { webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw); webRequest.downloadHandler = new DownloadHandlerBuffer(); webRequest.SetRequestHeader("Content-Type", "application/json"); yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError("Error: " + webRequest.error); } else { Debug.Log("Response: " + webRequest.downloadHandler.text); } } } ``` 这里使用了 UploadHandlerRaw 将请求体转换为 byte 数组,并设置了 Content-Type 为 application/json。同时使用 DownloadHandlerBuffer 接收服务器响应的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值