forked from kisence-mian/MyUnityFrameWork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPTool.cs
More file actions
129 lines (116 loc) · 4.93 KB
/
HTTPTool.cs
File metadata and controls
129 lines (116 loc) · 4.93 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using UnityEngine;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System;
using System.Net;
using System.IO;
using System.Threading;
public class HTTPTool
{
private static readonly Encoding DEFAULTENCODE = Encoding.UTF8;
public static void Upload_Request_Thread(string url, string file,CallBack<string> callBack = null)
{
NameValueCollection data = new NameValueCollection();
UpLoadThread ut = new UpLoadThread();
ut.url = url;
ut.files = new string[] { file };
ut.data = data;
ut.encoding = DEFAULTENCODE;
ut.callBack = callBack;
Thread th = new Thread(ut.Upload_Request);
th.Start();
}
public class UpLoadThread
{
public string url;
public string[] files;
public NameValueCollection data;
public Encoding encoding;
public CallBack<string> callBack;
public void Upload_Request()
{
Debug.Log("Upload_Request " + url);
try
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
//1.HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
request.Credentials = CredentialCache.DefaultCredentials;
request.Expect = null;
using (Stream stream = request.GetRequestStream())
{
//1.1 key/value
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
if (data != null)
{
foreach (string key in data.Keys)
{
stream.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, data[key]);
byte[] formitembytes = encoding.GetBytes(formitem);
stream.Write(formitembytes, 0, formitembytes.Length);
}
}
//1.2 file
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: text/plain\r\n\r\n";
byte[] buffer = new byte[4096];
int bytesRead = 0;
for (int i = 0; i < files.Length; i++)
{
stream.Write(boundarybytes, 0, boundarybytes.Length);
string header = string.Format(headerTemplate, "file", Path.GetFileName(files[i]));
byte[] headerbytes = encoding.GetBytes(header);
stream.Write(headerbytes, 0, headerbytes.Length);
using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
{
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
stream.Write(buffer, 0, bytesRead);
}
}
}
//1.3 form end
stream.Write(endbytes, 0, endbytes.Length);
}
//2.WebResponse
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
string result = stream.ReadToEnd();
if (result == "ok")
{
Debug.Log(files[0] + " 上传完成 " + result);
if(callBack != null)
{
callBack(files[0] + "上传完成 " + result);
}
}
else
{
Debug.Log(files[0] + "上传失败 " + result);
if (callBack != null)
{
callBack(files[0] + "上传失败 " + result);
}
}
return;
//return stream.ReadToEnd();
}
}
catch (Exception e)
{
Debug.Log(files[0] + "上传失败 \n" + e.ToString());
if (callBack != null)
{
callBack(files[0] + "上传失败 \n" + e.ToString());
}
}
}
}
}