TaiZhouChangChu/Assets/Script/GetQueryPractices.cs

73 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
public class GetQueryPractices : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GetPractices();
}
// Update is called once per frame
void Update()
{
}
public void GetPractices()
{
Dictionary<string, string> formDic = new Dictionary<string, string>();
formDic.Add("userId", "94");
string result = HttpWebRequestGet("http://124.71.203.22:8080/api/pro/queryPractices", formDic);
}
/// <summary>
/// 发送Get请求
/// </summary>
/// <param name="url">地址</param>
/// <param name="dic">请求参数定义</param>
/// <returns></returns>
public static string HttpWebRequestGet(string url, Dictionary<string, string> dic)
{
string result = "";
StringBuilder builder = new StringBuilder();
builder.Append(url);
if (dic.Count > 0)
{
builder.Append("?");
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
//添加参数
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
try
{
//获取内容
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
finally
{
stream.Close();
}
Debug.Log("PracticesResult" + result);
return result;
}
}