Mthod: Post
Link: www.link.com
Headers:
1: appKey="ABC"
2: Content-Type="text/csv"
如何编写C#脚本以通过发布发送数据?
答案 0 :(得分:0)
您可以执行以下操作:
void Start()
{
StartCoroutine(PostCrt());
}
IEnumerator PostCrt()
{
WWWForm form = new WWWForm();
form.AddField("appKey", "ABC");
form.AddField("Content-Type", "text/csv");
using (UnityWebRequest www = UnityWebRequest.Post("www.link.com", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Post Request Complete!");
}
}
}
别忘了调用“使用UnityEngine.Networking;”。才能使用UnityWebRequest。
编码愉快!
答案 1 :(得分:0)
您可以使用System.Net.HTTP命名空间:)
HttpClient client = new client
client.PostAsync("url",data)
在https://msdn.microsoft.com/en-us/library/hh138190(v=vs.118).aspx上引用
答案 2 :(得分:0)
public void Request()
{
try
{
string url = "www.link.com";
var request = UnityWebRequest.Post(url, "");
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Accept", "text/csv");
request.SetRequestHeader("appKey", "ABC");
StartCoroutine(onResponse(request));
}
catch (Exception e) { Debug.Log("ERROR : " + e.Message); }
}
private IEnumerator onResponse(UnityWebRequest req)
{
yield return req.SendWebRequest();
if (req.isError)
Debug.Log("Network error has occured: " + req.GetResponseHeader(""));
else
Debug.Log("Success "+req.downloadHandler.text );
byte[] results = req.downloadHandler.data;
Debug.Log("Second Success");
// Some code after success
}
此代码对我有用...