refact
parent
2f4ff1e774
commit
9adad9a327
@ -0,0 +1,70 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace nuget_cli
|
||||||
|
{
|
||||||
|
public class HttpClientServerQueryHandler
|
||||||
|
{
|
||||||
|
internal async Task UploadFilesToServerUsingHttpClient(
|
||||||
|
PushReport report, Uri uri,
|
||||||
|
FileInfo fi, string apikey)
|
||||||
|
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||||
|
|
||||||
|
// using (MultipartContent content = new MultipartContent("ascasc"))
|
||||||
|
|
||||||
|
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
||||||
|
{
|
||||||
|
using (HttpClient client = new HttpClient())
|
||||||
|
{
|
||||||
|
var dispo = new ContentDispositionHeaderValue("file")
|
||||||
|
{
|
||||||
|
FileName = fi.Name,
|
||||||
|
CreationDate = fi.CreationTime,
|
||||||
|
DispositionType = "form-data",
|
||||||
|
Size = fi.Length,
|
||||||
|
ModificationDate = fi.LastAccessTime
|
||||||
|
};
|
||||||
|
|
||||||
|
Stream fileStream = fi.OpenRead();
|
||||||
|
var streamcontent = new StreamContent(fileStream);
|
||||||
|
streamcontent.Headers.ContentDisposition = dispo;
|
||||||
|
formdata.Add(streamcontent, fi.Name, fi.Name);
|
||||||
|
|
||||||
|
// content.Add(formdata);
|
||||||
|
client.BaseAddress = uri;
|
||||||
|
HttpRequestMessage put = new HttpRequestMessage(HttpMethod.Put, uri)
|
||||||
|
{
|
||||||
|
Content = formdata
|
||||||
|
};
|
||||||
|
put.Headers.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
||||||
|
put.Headers.Add("X-NuGet-ApiKey", apikey);
|
||||||
|
put.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
|
||||||
|
HttpResponseMessage response = await client.SendAsync(put);
|
||||||
|
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
report.StatusCode = response.StatusCode.ToString();
|
||||||
|
var respstream = await response.Content.ReadAsStreamAsync();
|
||||||
|
var sr = new StreamReader(respstream);
|
||||||
|
report.Message = await sr.ReadToEndAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception rex)
|
||||||
|
{
|
||||||
|
report.Message = rex.Message;
|
||||||
|
report.StatusCode = "internal error";
|
||||||
|
Console.Error.WriteLine(rex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue