-1

I have created a web service using .NET Web API. The web service part works great, I am able to send the zip file using Advanced REST Client. The problem is that I can't send the zip file programmatically. Here is the Advanced REST Client Request Headers:

enter image description here

I have try some things but without success. I am not posting what I have try, considering that this is basic stuff for web developers (I am desktop developer), but if it is necessary, I will. Thank you in advance.

EDIT: This is my recent version

private async void BeginUpdate(bool webserverStatus)
{
    if (!webserverStatus) return;

    var httpClient = new HttpClient();
    var form = new MultipartFormDataContent();
    var byteArray = File.ReadAllBytes("myUpdatePackage.zip");

    form.Add(new ByteArrayContent(byteArray, 0, byteArray.Length), "myUpdatePackage", "myUpdatePackage.zip");
    HttpResponseMessage response = await httpClient.PostAsync(@"http://localhost:9000/api/file/", form);
    response.EnsureSuccessStatusCode();
    httpClient.Dispose();

    string sd = response.Content.ReadAsStringAsync().Result;
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Adnand
  • 562
  • 1
  • 8
  • 25
  • Yes, your code _is_ necessary, even if it sounds like basic stuff. – CodeCaster Jul 30 '18 at 09:29
  • 1
    Uses a sniffer like wireshark or fiddler. Compare the Advanced REST Client results with your apps results. Look at the http headers. Usually you have to add a missing header to your code. – jdweng Jul 30 '18 at 09:30
  • `webserverStatus` isn't a very good name as I can't really tell what it is. You should name it `webServerIsActive` or `webserverExists`, whatever the case may be. – AustinWBryan Jul 30 '18 at 13:12
  • Yes you are right, webserverStatus is just a helping variable for me. – Adnand Jul 30 '18 at 13:24

1 Answers1

0

I realized that on Advanced REST Client, I was it using PUT, meanwhile on my C# application I call it using POST

HttpResponseMessage response = await httpClient.PostAsync(@"http://localhost:9000/api/file/", form);

So, here is the complete code:

private async void BeginUpdate(bool webserverStatus)
{
    if (!webserverStatus) return;

    var byteArray  = File.ReadAllBytes("myUpdatePackage.zip");
    var httpClient = new HttpClient();
    var form       = new MultipartFormDataContent();

    form.Add(new ByteArrayContent(byteArray, 0, byteArray.Length), "myUpdatePackage", "myUpdatePackage.zip");
    HttpResponseMessage response = await httpClient.PutAsync(@"http://localhost:9000/api/file/", form);

    response.EnsureSuccessStatusCode();
    httpClient.Dispose();
    string sd = response.Content.ReadAsStringAsync().Result;
}

More details on this question

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Adnand
  • 562
  • 1
  • 8
  • 25