using async methods
This commit is contained in:
parent
6f82a0a00f
commit
451d810ade
1 changed files with 9 additions and 10 deletions
|
|
@ -17,7 +17,7 @@ namespace nuget_cli
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal async Task UploadFilesToServerAsync(PushReport report, Uri uri, FileInfo fi,
|
internal async Task UploadFilesToServerAsync(PushReport report, Uri uri, FileInfo fi,
|
||||||
string apikey)
|
string apikey)
|
||||||
|
|
@ -46,7 +46,7 @@ namespace nuget_cli
|
||||||
fileheaderbytes.Length + fi.Length + endBoundaryBytes.Length;
|
fileheaderbytes.Length + fi.Length + endBoundaryBytes.Length;
|
||||||
|
|
||||||
|
|
||||||
httpWebRequest.BeginGetRequestStream((result) =>
|
httpWebRequest.BeginGetRequestStream(async (result) =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -54,22 +54,21 @@ namespace nuget_cli
|
||||||
|
|
||||||
using (Stream requestStream = request.EndGetRequestStream(result))
|
using (Stream requestStream = request.EndGetRequestStream(result))
|
||||||
{
|
{
|
||||||
WriteToStream(requestStream, boundarybytes, boundarybytes.Length);
|
await WriteToStream(requestStream, boundarybytes, boundarybytes.Length);
|
||||||
WriteToStream(requestStream, fileheaderbytes, fileheaderbytes.Length);
|
await WriteToStream(requestStream, fileheaderbytes, fileheaderbytes.Length);
|
||||||
using (var fss = fi.OpenRead())
|
using (var fss = fi.OpenRead())
|
||||||
{
|
{
|
||||||
byte[] buffer = new byte[TXLEN];
|
byte[] buffer = new byte[TXLEN];
|
||||||
var form_bytes_read = fss.Read(buffer, 0, TXLEN);
|
var form_bytes_read = fss.Read(buffer, 0, TXLEN);
|
||||||
while (form_bytes_read > 0)
|
while (form_bytes_read > 0)
|
||||||
{
|
{
|
||||||
WriteToStream(requestStream, buffer, form_bytes_read);
|
await WriteToStream(requestStream, buffer, form_bytes_read);
|
||||||
form_bytes_read = fss.Read(buffer, 0, TXLEN);
|
form_bytes_read = fss.Read(buffer, 0, TXLEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
||||||
requestStream.Close();
|
requestStream.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception rex)
|
catch (Exception rex)
|
||||||
{
|
{
|
||||||
|
|
@ -82,13 +81,13 @@ namespace nuget_cli
|
||||||
}
|
}
|
||||||
}, httpWebRequest);
|
}, httpWebRequest);
|
||||||
|
|
||||||
WebResponse resp = httpWebRequest.GetResponse();
|
WebResponse resp = await httpWebRequest.GetResponseAsync();
|
||||||
|
|
||||||
Stream stream = resp.GetResponseStream();
|
Stream stream = resp.GetResponseStream();
|
||||||
StreamReader re = new StreamReader(stream);
|
StreamReader re = new StreamReader(stream);
|
||||||
if (resp is HttpWebResponse)
|
if (resp is HttpWebResponse)
|
||||||
{
|
{
|
||||||
String json = re.ReadToEnd();
|
String json = await re.ReadToEndAsync();
|
||||||
report.Message = json;
|
report.Message = json;
|
||||||
var hrep = resp as HttpWebResponse;
|
var hrep = resp as HttpWebResponse;
|
||||||
report.StatusCode = hrep.StatusCode.ToString();
|
report.StatusCode = hrep.StatusCode.ToString();
|
||||||
|
|
@ -101,9 +100,9 @@ namespace nuget_cli
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes byte array to stream. Author : Farhan Ghumra
|
/// Writes byte array to stream. Author : Farhan Ghumra
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static void WriteToStream(Stream s, byte[] bytes, int len)
|
private static async Task WriteToStream(Stream s, byte[] bytes, int len)
|
||||||
{
|
{
|
||||||
s.Write(bytes, 0, len);
|
await s.WriteAsync(bytes, 0, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue