| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using UnityEngine;
- using UnityEngine.UI;
- public static class HttpRequest
- {
- public static string SendHttpRequest(string headersData, string postData, string url, string contentType)
- {
- /* byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
- webRequest.Method = "POST";
- webRequest.ContentType = contentType;
- webRequest.ContentLength = byteArray.Length;
- webRequest.Headers.Add("x_auth_token", headersData);
- //webRequest.Timeout = 5000;
- 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);
- int code = response.GetHashCode();
- Debug.Log("状态码:" + code);
- //Debug.Log("响应我的消息:" + sr.ReadToEnd());
- string str = sr.ReadToEnd();
- return str;*/
- try
- {
- byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
- webRequest.Method = "POST";
- webRequest.ContentType = contentType;
- webRequest.ContentLength = byteArray.Length;
- webRequest.Headers.Add("x_auth_token", headersData);
- webRequest.Timeout = 2000;
- 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);
- //检验状态码,如果成功接收数据
- int code = response.GetHashCode();
- Debug.Log("code: " + code);
- /* if (code == 200)
- {*/
- string str = sr.ReadToEnd();
- Debug.Log("str: " + str);
- webRequest.Abort();
- response.Close();
- sr.Close();
- return str;
- /* }*/
- }
- catch (Exception e)
- {
- e.ToString();
- Debug.Log("e: " + e.ToString());
- return null;
- }
- }
- }
|