Skip to content

Commit 76a9b46

Browse files
authored
Merge pull request #226 from qiniu/fix/dotnetcore2_0
支持新版分片上传
2 parents 78eafa2 + d09258d commit 76a9b46

File tree

11 files changed

+635
-53
lines changed

11 files changed

+635
-53
lines changed

src/Qiniu/Http/HttpManager.cs

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ public HttpResult PostJson(string url, string data, string token, bool binaryMod
530530
wReq.UserAgent = userAgent;
531531
wReq.AllowAutoRedirect = allowAutoRedirect;
532532
wReq.ServicePoint.Expect100Continue = false;
533-
533+
534534
if (data != null)
535535
{
536536
wReq.AllowWriteStreamBuffering = true;
@@ -1210,6 +1210,128 @@ public HttpResult PostMultipart(string url, byte[] data, string boundary, string
12101210
return result;
12111211
}
12121212

1213+
/// <summary>
1214+
/// HTTP-PUT方法(包含body数据, headers)
1215+
/// </summary>
1216+
/// <param name="url">请求目标URL</param>
1217+
/// <param name="data">主体数据(字节数据)</param>
1218+
/// <param name="headers">上传设置的headers</param>
1219+
/// <param name="token">令牌(凭证)[可选->设置为null]</param>
1220+
/// <param name="binaryMode">是否以二进制模式读取响应内容(默认:否,即表示以文本方式读取)</param>
1221+
/// <returns>HTTP-PUT的响应结果</returns>
1222+
public HttpResult PutDataWithHeaders(string url, byte[] data, Dictionary<string, string> headers, bool binaryMode = false)
1223+
{
1224+
HttpResult result = new HttpResult();
1225+
1226+
HttpWebRequest wReq = null;
1227+
1228+
try
1229+
{
1230+
wReq = WebRequest.Create(url) as HttpWebRequest;
1231+
wReq.Method = "PUT";
1232+
wReq.ContentType = ContentType.APPLICATION_OCTET_STREAM;
1233+
wReq.UserAgent = userAgent;
1234+
wReq.AllowAutoRedirect = allowAutoRedirect;
1235+
wReq.ServicePoint.Expect100Continue = false;
1236+
1237+
foreach(KeyValuePair<string, string> header in headers)
1238+
{
1239+
if (!string.IsNullOrEmpty(header.Value))
1240+
{
1241+
wReq.Headers.Add(header.Key, header.Value);
1242+
}
1243+
}
1244+
1245+
if (data != null)
1246+
{
1247+
wReq.AllowWriteStreamBuffering = true;
1248+
using (Stream sReq = wReq.GetRequestStream())
1249+
{
1250+
sReq.Write(data, 0, data.Length);
1251+
sReq.Flush();
1252+
}
1253+
}
1254+
1255+
HttpWebResponse wResp = wReq.GetResponse() as HttpWebResponse;
1256+
1257+
if (wResp != null)
1258+
{
1259+
result.Code = (int)wResp.StatusCode;
1260+
result.RefCode = (int)wResp.StatusCode;
1261+
1262+
getHeaders(ref result, wResp);
1263+
1264+
if (binaryMode)
1265+
{
1266+
int len = (int)wResp.ContentLength;
1267+
result.Data = new byte[len];
1268+
int bytesLeft = len;
1269+
int bytesRead = 0;
1270+
1271+
using (BinaryReader br = new BinaryReader(wResp.GetResponseStream()))
1272+
{
1273+
while (bytesLeft > 0)
1274+
{
1275+
bytesRead = br.Read(result.Data, len - bytesLeft, bytesLeft);
1276+
bytesLeft -= bytesRead;
1277+
}
1278+
}
1279+
}
1280+
else
1281+
{
1282+
using (StreamReader sr = new StreamReader(wResp.GetResponseStream()))
1283+
{
1284+
result.Text = sr.ReadToEnd();
1285+
}
1286+
}
1287+
1288+
wResp.Close();
1289+
}
1290+
}
1291+
catch (WebException wex)
1292+
{
1293+
HttpWebResponse xResp = wex.Response as HttpWebResponse;
1294+
if (xResp != null)
1295+
{
1296+
result.Code = (int)xResp.StatusCode;
1297+
result.RefCode = (int)xResp.StatusCode;
1298+
1299+
getHeaders(ref result, xResp);
1300+
1301+
using (StreamReader sr = new StreamReader(xResp.GetResponseStream()))
1302+
{
1303+
result.Text = sr.ReadToEnd();
1304+
}
1305+
1306+
xResp.Close();
1307+
}
1308+
}
1309+
catch (Exception ex)
1310+
{
1311+
StringBuilder sb = new StringBuilder();
1312+
sb.AppendFormat("[{0}] [{1}] [HTTP-PUT-BIN] Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"),userAgent);
1313+
Exception e = ex;
1314+
while (e != null)
1315+
{
1316+
sb.Append(e.Message + " ");
1317+
e = e.InnerException;
1318+
}
1319+
sb.AppendLine();
1320+
1321+
result.RefCode = (int)HttpCode.USER_UNDEF;
1322+
result.RefText += sb.ToString();
1323+
}
1324+
finally
1325+
{
1326+
if (wReq != null)
1327+
{
1328+
wReq.Abort();
1329+
}
1330+
}
1331+
1332+
return result;
1333+
}
1334+
12131335
/// <summary>
12141336
/// 获取返回信息头
12151337
/// </summary>

src/Qiniu/Storage/PutExtra.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,15 @@ public class PutExtra
3737
/// 块并发上传的线程数量
3838
/// </summary>
3939
public int BlockUploadThreads { set; get; }
40-
}
40+
41+
/// <summary>
42+
/// 分片上传版本 目前支持v1/v2版本 默认v1
43+
/// </summary>
44+
public string Version = "v1";
45+
46+
/// <summary>
47+
/// 分片上传v2字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
48+
/// </summary>
49+
public int PartSize = 4 * 1024 * 1024;
4150
}
51+
}

0 commit comments

Comments
 (0)