Problem sending photo to API

Mehran

Member
Joined
Aug 18, 2021
Messages
7
Programming Experience
Beginner
Hello Good time !
I want to send a image file to the following address


1.png

I tested it with Postman and it works..
I wrote the following code

C#:
try
            {
                string URI = "https://admin.asilasal.ir/api/Guestuploadimage";
                string myParameters = "file=" + @"C:\Users\Administrator\Desktop\1.jpg";

                using (WebClient wc = new WebClient())
                {
                    wc.Headers[HttpRequestHeader.ContentType] = "multipart/form-data; boundary=<calculated when request is sent>";
                    wc.Headers.Set("Content-Length", "<calculated when request is sent>");
                    wc.Headers.Set("HOST", "<calculated when request is sent>");
                    wc.Headers.Set("User-Agent", "<PostmanRuntime/7.28.4>");
                    wc.Headers.Set("User-Agent", "*/*");
                    wc.Headers.Set("Accept", "*/*");
                    wc.Headers.Set("Accept-Encoding", "gzip, deflate, br");
                    wc.Headers.Set("Connection", "keep-alive");



                    string HtmlResult = wc.UploadString(URI, myParameters);
                    //MessageBox.Show(HtmlResult);
                }
            }
            catch (Exception ex)
            {
                textBox1.Text = ex.Message;
            }

But I get the following error :"An exception occurred during a WebClient request."
Please note: I also use this code in older systems (.net 3.5,4) so I used WebClient
 

Attachments

  • 20.png
    20.png
    38 KB · Views: 16
The UploadString method is, as the name suggests, for uploading a string, i.e. text data. If you want to upload a file then the obvious choice is to call UploadFile.
 
Moving out of WinForms forum since this is not a WinForms issue.
 
Back
Top Bottom