ex to output
This commit is contained in:
8
.build
8
.build
@ -16,9 +16,9 @@
|
|||||||
<delete dir="test/isnd.tests/obj/" failonerror="false" />
|
<delete dir="test/isnd.tests/obj/" failonerror="false" />
|
||||||
</target>
|
</target>
|
||||||
<target name="build" description="build all">
|
<target name="build" description="build all">
|
||||||
<solution configuration="release">
|
<exec program="dotnet" commandline="build" />
|
||||||
<referenceprojects>
|
</target>
|
||||||
</referenceprojects>
|
<target name="test" description="build all">
|
||||||
</solution>
|
<exec program="dotnet" commandline="test" />
|
||||||
</target>
|
</target>
|
||||||
</project>
|
</project>
|
||||||
|
@ -9,43 +9,50 @@ namespace isn
|
|||||||
{
|
{
|
||||||
public static class UploadFilesToServerUsingHttpClient
|
public static class UploadFilesToServerUsingHttpClient
|
||||||
{
|
{
|
||||||
public static PushReport UploadFilesToServer(Uri uri, FileInfo fi,
|
public static PushReport UploadFilesToServer(this HttpClient client, Uri uri, FileInfo fi,
|
||||||
|
string apikey)
|
||||||
|
{
|
||||||
|
return UploadFilesToServerAsync(client, uri, fi, apikey).Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<PushReport> UploadFilesToServerAsync(this HttpClient client, Uri uri, FileInfo fi,
|
||||||
string apikey)
|
string apikey)
|
||||||
{
|
{
|
||||||
using (var client = new HttpClient())
|
|
||||||
|
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
||||||
|
client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
||||||
|
|
||||||
|
using (var multipartFormDataContent = new MultipartFormDataContent())
|
||||||
{
|
{
|
||||||
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
/* var values = new[]
|
||||||
client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
{
|
||||||
using (var multipartFormDataContent = new MultipartFormDataContent())
|
new KeyValuePair<string, string>("Id", Guid.NewGuid().ToString()),
|
||||||
|
new KeyValuePair<string, string>("Key", "awesome"),
|
||||||
|
new KeyValuePair<string, string>("From", "khalid@home.com")
|
||||||
|
//other values
|
||||||
|
};foreach (var keyValuePair in values)
|
||||||
|
{
|
||||||
|
multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
|
||||||
|
String.Format("\"{0}\"", keyValuePair.Key));
|
||||||
|
} */
|
||||||
|
multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)),
|
||||||
|
'"' + "File" + '"',
|
||||||
|
'"' + fi.Name + '"');
|
||||||
|
|
||||||
|
var result = await client.PutAsync(uri, multipartFormDataContent);
|
||||||
|
|
||||||
|
if (result.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
/* var values = new[]
|
string report = await result.Content.ReadAsStringAsync();
|
||||||
{
|
Console.WriteLine(report);
|
||||||
new KeyValuePair<string, string>("Id", Guid.NewGuid().ToString()),
|
|
||||||
new KeyValuePair<string, string>("Key", "awesome"),
|
|
||||||
new KeyValuePair<string, string>("From", "khalid@home.com")
|
|
||||||
//other values
|
|
||||||
};foreach (var keyValuePair in values)
|
|
||||||
{
|
|
||||||
multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
|
|
||||||
String.Format("\"{0}\"", keyValuePair.Key));
|
|
||||||
} */
|
|
||||||
multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)),
|
|
||||||
'"' + "File" + '"',
|
|
||||||
'"' + fi.Name + '"');
|
|
||||||
|
|
||||||
var result = client.PutAsync(uri, multipartFormDataContent).Result;
|
|
||||||
result.EnsureSuccessStatusCode();
|
|
||||||
if (result.IsSuccessStatusCode)
|
|
||||||
{
|
|
||||||
Task.Run(async ()=>
|
|
||||||
{
|
|
||||||
string report = await result.Content.ReadAsStringAsync();
|
|
||||||
Console.WriteLine(report);
|
|
||||||
}).Wait();
|
|
||||||
|
|
||||||
}
|
|
||||||
return new PushReport();
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string ereport = await result.Content.ReadAsStringAsync();
|
||||||
|
Console.WriteLine(ereport);
|
||||||
|
}
|
||||||
|
return new PushReport();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ using System;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
@ -30,47 +31,22 @@ namespace isn
|
|||||||
};
|
};
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
|
using (var client = new HttpClient())
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine("Connecting to "+ pubRes.Id);
|
Console.WriteLine("Connecting to "+ pubRes.Id);
|
||||||
return UploadFilesToServerUsingHttpClient.UploadFilesToServer(new Uri(pubRes.Id), fi, apikey);
|
return client.UploadFilesToServer(new Uri(pubRes.Id), fi, apikey);
|
||||||
}
|
}
|
||||||
catch (WebException ex)
|
catch (HttpRequestException hrex)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine(ex.Message);
|
|
||||||
var report = new PushReport
|
var report = new PushReport
|
||||||
{
|
{
|
||||||
PkgName = fi.Name
|
PkgName = fi.Name,
|
||||||
|
Message = "HttpRequest: " + hrex.Message,
|
||||||
|
StackTrace = hrex.StackTrace,
|
||||||
|
StatusCode = hrex.HResult.ToString()
|
||||||
};
|
};
|
||||||
report.StatusCode = ex.Status.ToString();
|
Console.Error.WriteLine(hrex.Message);
|
||||||
report.OK = false;
|
|
||||||
if (ex.Response != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (Stream respStream = ex.Response.GetResponseStream())
|
|
||||||
{
|
|
||||||
StreamReader sr = new StreamReader(respStream);
|
|
||||||
string json = sr.ReadToEnd();
|
|
||||||
var res = JsonConvert.DeserializeObject<IsndErrorMessage>(json);
|
|
||||||
report.Message = res.msg;
|
|
||||||
|
|
||||||
// ecode == 1 => package already present server side.
|
|
||||||
report.AlreadyPresent = res.ecode == 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception iex)
|
|
||||||
{
|
|
||||||
report.Message = iex.Message;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
report.Message = ex.Message;
|
|
||||||
}
|
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -82,7 +58,7 @@ namespace isn
|
|||||||
StackTrace = ex.StackTrace
|
StackTrace = ex.StackTrace
|
||||||
};
|
};
|
||||||
Console.Error.WriteLine(ex.Message);
|
Console.Error.WriteLine(ex.Message);
|
||||||
throw;
|
return report;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user