Reputation: 179
I have an asp.net page that can upload files to a server, but I need to read all folders in a CD-ROM and upload all of them to this page. I made a program in WPF but I always receive a 401 error (Unauthorized), how could I achive this?
I have this code to upload the file:
foreach (var archivoMS in ArchivosMS) {
var ruta = Helper.Route +
"?action=save" +
"&id=" + archivoMS.Id +
"&idType=" + archivoMS.IdType +
"&name=" + archivoMS.Name;
FileStream fileStream = File.OpenRead(archivoMS.NameWithRoute);
HttpContent fileStreamContent = new StreamContent(fileStream);
using (var client = new HttpClient()) {
client.BaseAddress = new Uri(Helper.Route );
using (var formData = new MultipartFormDataContent()) {
formData.Add(fileStreamContent, archivoMS.Name, archivoMS.Name);
var response = client.PostAsync(ruta, formData).Result;
if (!response.IsSuccessStatusCode) {
}
}
}
}
Upvotes: 0
Views: 526
Reputation: 429
Try adding a handler with useDefaultCredentials to your httpclient. It may not be sending your credentials at all.
HttpClientHandler handler = new HttpClientHandler(); handler.UseDefaultCredentials = true; HttpClient client = new HttpClient(handler);
Upvotes: 1