Unity C# 网络学习(七)——HTTP(二)
一.Post请求
HttpWebRequest httpWebRequest = WebRequest.CreateHttp("http://192.168.1.103:8080/Http_Server/");
httpWebRequest.Method = WebRequestMethods.Http.Post;
httpWebRequest.Timeout = 2000;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
string str = "Name=zzs&ID=2";
byte[] buffer = Encoding.UTF8.GetBytes(str);
httpWebRequest.ContentLength = buffer.Length;
Stream s = httpWebRequest.GetRequestStream();
s.Write(buffer,0,buffer.Length);
s.Close();
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
if (httpWebResponse == null)
return;
Debug.Log(httpWebResponse.StatusCode);
httpWebResponse.Close();
二.HTTP上传数据
private void Start()
{
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(
new Uri("http://192.168.1.103:8080/Http_Server/"));
httpWebRequest.Method = WebRequestMethods.Http.Post;
httpWebRequest.Timeout = 5000 * 100;
httpWebRequest.Credentials = new NetworkCredential("zzs", "123");
httpWebRequest.PreAuthenticate = true;
httpWebRequest.ContentType = "multipart/form-data;boundary=zzs";
string head = "--zzs\r\n" +
"Content-Disposition:form-data;name=\"file\";filename=\"http上传的文件.jpg\"\r\n" +
"Content-Type:application/octet-stream\r\n\r\n";
byte[] headBytes = Encoding.UTF8.GetBytes(head);
byte[] endBytes = Encoding.UTF8.GetBytes("\r\n--zzs--\r\n");
using (FileStream fs = new FileStream(Application.streamingAssetsPath + "/test.jpg", FileMode.Open))
{
byte[] buffer = new byte[10240];
int len = fs.Read(buffer, 0, buffer.Length);
Stream s = httpWebRequest.GetRequestStream();
s.Write(headBytes, 0, headBytes.Length);
while (len > 0)
{
s.Write(buffer, 0, len);
len = fs.Read(buffer, 0, buffer.Length);
}
s.Write(endBytes, 0, endBytes.Length);
s.Close();
}
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
if (httpWebResponse == null)
return;
Debug.Log(httpWebResponse.StatusCode == HttpStatusCode.OK ? "上传成功!" : "上传失败!");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
三.HTTP上传数据封装
public async void UpLoadFile(string fileName, string path, Action<HttpStatusCode> action)
{
HttpStatusCode code = HttpStatusCode.OK;
await Task.Run(() =>
{
try
{
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(new Uri(RootPath));
httpWebRequest.Method = WebRequestMethods.Http.Post;
httpWebRequest.Timeout = 5000 * 100;
httpWebRequest.Credentials = new NetworkCredential(UserName, Password);
httpWebRequest.PreAuthenticate = true;
httpWebRequest.ContentType = "multipart/form-data;boundary=zzs";
string head = "--zzs\r\n" +
"Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\n" +
"Content-Type:application/octet-stream\r\n\r\n";
head = string.Format(head, fileName);
byte[] headBytes = Encoding.UTF8.GetBytes(head);
byte[] endBytes = Encoding.UTF8.GetBytes("\r\n--zzs--\r\n");
using (FileStream fs = new FileStream(path, FileMode.Open))
{
Stream s = httpWebRequest.GetRequestStream();
byte[] buffer = new byte[10240];
int len = fs.Read(buffer, 0, buffer.Length);
s.Write(headBytes,0,headBytes.Length);
while (len>0)
{
s.Write(buffer,0,len);
len = fs.Read(buffer, 0, buffer.Length);
}
s.Write(endBytes,0,endBytes.Length);
s.Close();
}
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
if (httpWebResponse == null)
return;
code = httpWebResponse.StatusCode;
}
catch (WebException we)
{
Debug.Log(we);
code = HttpStatusCode.InternalServerError;
}
});
action?.Invoke(code);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51