using web request
This commit is contained in:
parent
9adad9a327
commit
d7cabaad8b
8 changed files with 163 additions and 33 deletions
7
src/nuget-cli/Constants.cs
Normal file
7
src/nuget-cli/Constants.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace nuget_cli
|
||||||
|
{
|
||||||
|
internal static class Constants
|
||||||
|
{
|
||||||
|
internal const string ClientVersion = "nuget-cli v1.0";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace nuget_cli
|
|
||||||
{
|
|
||||||
public static class Constants
|
|
||||||
{
|
|
||||||
public const string ClientVersion = "nuget_cli v1.0";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -7,8 +7,6 @@ namespace nuget_cli
|
||||||
|
|
||||||
partial class Program
|
partial class Program
|
||||||
{
|
{
|
||||||
static readonly ServerQueryHandler serverQueryHandler;
|
|
||||||
|
|
||||||
private static void SourceList(IEnumerable<string> sargs)
|
private static void SourceList(IEnumerable<string> sargs)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
|
@ -30,7 +28,7 @@ namespace nuget_cli
|
||||||
|
|
||||||
foreach (string pkg in pkgs)
|
foreach (string pkg in pkgs)
|
||||||
{
|
{
|
||||||
var report = await PushCommand.RunAsync(serverQueryHandler,pkg,source,apiKey);
|
var report = await PushCommand.RunAsync(pkg,source,apiKey);
|
||||||
|
|
||||||
pushReports.Add(report);
|
pushReports.Add(report);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@ namespace nuget_cli
|
||||||
|
|
||||||
static Program()
|
static Program()
|
||||||
{
|
{
|
||||||
serverQueryHandler = new ServerQueryHandler();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ namespace nuget_cli
|
||||||
{
|
{
|
||||||
internal class PushCommand
|
internal class PushCommand
|
||||||
{
|
{
|
||||||
static internal async Task<PushReport> RunAsync(ServerQueryHandler queryHandler, string pkg, string source, string apikey)
|
static internal async Task<PushReport> RunAsync(string pkg, string source, string apikey)
|
||||||
{
|
{
|
||||||
FileInfo fi = new FileInfo(pkg);
|
FileInfo fi = new FileInfo(pkg);
|
||||||
var report = new PushReport
|
var report = new PushReport
|
||||||
|
|
@ -25,7 +25,13 @@ namespace nuget_cli
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await queryHandler.UploadFilesToServer(report, new Uri(source), fi, apikey);
|
var wrqueryHandler = new UploadFilesToServerUsingWebRequest();
|
||||||
|
await wrqueryHandler.UploadFilesToServerAsync(report, new Uri(source), fi, apikey);
|
||||||
|
#if FAILS
|
||||||
|
var hcqueryHandler = new HttpClientServerQueryHandler();
|
||||||
|
await hcqueryHandler.UploadFilesToServerAsync(report, new Uri(source), fi, apikey);
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (WebException ex)
|
catch (WebException ex)
|
||||||
{
|
{
|
||||||
|
|
@ -39,6 +45,8 @@ namespace nuget_cli
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
report.Message = ex.Message;
|
||||||
|
report.StackTrace = ex.StackTrace;
|
||||||
await Console.Error.WriteLineAsync(ex.Message);
|
await Console.Error.WriteLineAsync(ex.Message);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,10 @@ namespace nuget_cli
|
||||||
public class PushReport
|
public class PushReport
|
||||||
{
|
{
|
||||||
public string PkgName { get; set; }
|
public string PkgName { get; set; }
|
||||||
public bool Executed { get; set; }
|
public bool Executed { get; internal set; }
|
||||||
public bool AlreadyPresent { get; set; }
|
public bool AlreadyPresent { get; internal set; }
|
||||||
public string Message { get; set; }
|
public string Message { get; internal set; }
|
||||||
public string StatusCode { get; set; }
|
public string StatusCode { get; internal set; }
|
||||||
|
public string StackTrace { get; internal set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -9,7 +9,7 @@ namespace nuget_cli
|
||||||
{
|
{
|
||||||
public class HttpClientServerQueryHandler
|
public class HttpClientServerQueryHandler
|
||||||
{
|
{
|
||||||
internal async Task UploadFilesToServerUsingHttpClient(
|
internal async Task UploadFilesToServerAsync(
|
||||||
PushReport report, Uri uri,
|
PushReport report, Uri uri,
|
||||||
FileInfo fi, string apikey)
|
FileInfo fi, string apikey)
|
||||||
|
|
||||||
|
|
@ -18,8 +18,6 @@ namespace nuget_cli
|
||||||
{
|
{
|
||||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||||
|
|
||||||
// using (MultipartContent content = new MultipartContent("ascasc"))
|
|
||||||
|
|
||||||
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
||||||
{
|
{
|
||||||
using (HttpClient client = new HttpClient())
|
using (HttpClient client = new HttpClient())
|
||||||
|
|
@ -36,9 +34,8 @@ namespace nuget_cli
|
||||||
Stream fileStream = fi.OpenRead();
|
Stream fileStream = fi.OpenRead();
|
||||||
var streamcontent = new StreamContent(fileStream);
|
var streamcontent = new StreamContent(fileStream);
|
||||||
streamcontent.Headers.ContentDisposition = dispo;
|
streamcontent.Headers.ContentDisposition = dispo;
|
||||||
formdata.Add(streamcontent, fi.Name, fi.Name);
|
formdata.Add(streamcontent, "file", fi.Name);
|
||||||
|
|
||||||
// content.Add(formdata);
|
|
||||||
client.BaseAddress = uri;
|
client.BaseAddress = uri;
|
||||||
HttpRequestMessage put = new HttpRequestMessage(HttpMethod.Put, uri)
|
HttpRequestMessage put = new HttpRequestMessage(HttpMethod.Put, uri)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
138
src/nuget-cli/UploadFilesToServerUsingWebRequest.cs
Normal file
138
src/nuget-cli/UploadFilesToServerUsingWebRequest.cs
Normal file
|
|
@ -0,0 +1,138 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace nuget_cli
|
||||||
|
{
|
||||||
|
public class UploadFilesToServerUsingWebRequest
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
||||||
|
/// </summary>
|
||||||
|
internal async Task UploadFilesToServerAsync(PushReport report, Uri uri, FileInfo fi,
|
||||||
|
string apikey)
|
||||||
|
{
|
||||||
|
|
||||||
|
// string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
|
||||||
|
const int TXLEN = 0x1000;
|
||||||
|
/// the form-data file upload, properly formatted
|
||||||
|
string fileheaderTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n";
|
||||||
|
|
||||||
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||||
|
|
||||||
|
// "X-NuGet-ApiKey
|
||||||
|
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
|
||||||
|
byte[] boudaryByte = Encoding.ASCII.GetBytes(boundary);
|
||||||
|
byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
|
||||||
|
string fileheader = string.Format(fileheaderTemplate, "file", fi.Name, "application/octet-stream");
|
||||||
|
byte[] fileheaderbytes = Encoding.ASCII.GetBytes(fileheader);
|
||||||
|
|
||||||
|
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
|
||||||
|
|
||||||
|
httpWebRequest.Method = "PUT";
|
||||||
|
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
|
||||||
|
httpWebRequest.AllowAutoRedirect = false;
|
||||||
|
httpWebRequest.Headers.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
||||||
|
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
|
||||||
|
httpWebRequest.ContentLength = boudaryByte.Length + fileheaderbytes.Length + fi.Length + trailer.Length;
|
||||||
|
|
||||||
|
|
||||||
|
httpWebRequest.BeginGetRequestStream((result) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
|
||||||
|
|
||||||
|
using (Stream requestStream = request.EndGetRequestStream(result))
|
||||||
|
{
|
||||||
|
WriteToStream(requestStream, boudaryByte, boudaryByte.Length);
|
||||||
|
WriteToStream(requestStream, fileheaderbytes, fileheaderbytes.Length);
|
||||||
|
// WriteToStream(s, string.Format(fileheaderlentemplate, fi.Length));
|
||||||
|
/// Write the file data to the stream.
|
||||||
|
using (var fss = fi.OpenRead())
|
||||||
|
{
|
||||||
|
byte[] buffer = new byte[TXLEN];
|
||||||
|
var form_bytes_read = fss.Read(buffer, 0, TXLEN);
|
||||||
|
while (form_bytes_read > 0)
|
||||||
|
{
|
||||||
|
WriteToStream(requestStream, buffer, form_bytes_read);
|
||||||
|
form_bytes_read = fss.Read(buffer, 0, TXLEN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
requestStream.Write(trailer, 0, trailer.Length);
|
||||||
|
//requestStream.Close();
|
||||||
|
}
|
||||||
|
request.BeginGetResponse(a =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = request.EndGetResponse(a);
|
||||||
|
var responseStream = response.GetResponseStream();
|
||||||
|
using (var sr = new StreamReader(responseStream))
|
||||||
|
{
|
||||||
|
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
|
||||||
|
{
|
||||||
|
string responseString = streamReader.ReadToEnd();
|
||||||
|
//responseString is depend upon your web service.
|
||||||
|
if (responseString == "Success")
|
||||||
|
{
|
||||||
|
report.Message = "stored successfully on server.";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
report.Message = "Error occurred while uploading packet on server.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
report.Message = ex.Message;
|
||||||
|
Console.Error.WriteLine(ex.Message);
|
||||||
|
#if DEBUG
|
||||||
|
Console.Error.WriteLine("Stack trace:");
|
||||||
|
Console.Error.WriteLine(ex.StackTrace);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}, null);
|
||||||
|
}
|
||||||
|
catch (Exception rex)
|
||||||
|
{
|
||||||
|
report.Message = rex.Message;
|
||||||
|
Console.Error.WriteLine(rex.Message);
|
||||||
|
#if DEBUG
|
||||||
|
Console.Error.WriteLine("Stack trace:");
|
||||||
|
Console.Error.WriteLine(rex.StackTrace);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}, httpWebRequest);
|
||||||
|
|
||||||
|
WebResponse resp = httpWebRequest.GetResponse();
|
||||||
|
Stream stream = resp.GetResponseStream();
|
||||||
|
StreamReader re = new StreamReader(stream);
|
||||||
|
String json = re.ReadToEnd();
|
||||||
|
report.Message = json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes string to stream. Author : Farhan Ghumra
|
||||||
|
/// </summary>
|
||||||
|
private static void WriteToStream(Stream s, string txt)
|
||||||
|
{
|
||||||
|
byte[] bytes = Encoding.UTF8.GetBytes(txt);
|
||||||
|
s.Write(bytes, 0, bytes.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes byte array to stream. Author : Farhan Ghumra
|
||||||
|
/// </summary>
|
||||||
|
private static void WriteToStream(Stream s, byte[] bytes, int len)
|
||||||
|
{
|
||||||
|
s.Write(bytes, 0, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue