c# - Post data using MultipartFormDataContent from HttpClient -


i have webapi service reads post data including file data using

httpcontext.request key value pairs httpcontext.current.request["loadid"] 

now trying write , console application using httpclient not able make work

private static bool addexception(string vin, string loadid, string sessionid)         {             var client = new httpclient             {                 baseaddress = new uri("url")             };             servicepointmanager.servercertificatevalidationcallback = (s, cert, chain, ssl) => true;              const string quickcode = "01-01-1";              const string inspectiontype = "loading";             const string inspectorcode = "001";             const string numberofimages = "1";             const string imagenumber = "1";             const string exceptiontype = "driver";             const string imagetype = "exception";             var date = datetime.now.tostring();              var content = new multipartformdatacontent();             var values = new[]                     {                         new keyvaluepair<string, string>("loadid", loadid),                         new keyvaluepair<string, string>("vin", vin),                         new keyvaluepair<string, string>("sessionid", sessionid),                         new keyvaluepair<string, string>("quickcode", quickcode),                         new keyvaluepair<string, string>("strinspectiontype", inspectiontype),                         new keyvaluepair<string, string>("inspectorcode", inspectorcode),                         new keyvaluepair<string, string>("noofimages", numberofimages),                         new keyvaluepair<string, string>("imageno", imagenumber),                         new keyvaluepair<string, string>("strexceptiontype", exceptiontype),                         new keyvaluepair<string, string>("imagetype", imagetype),                         new keyvaluepair<string, string>("datetimeoffset", date)                     };             var filecontent = new bytearraycontent(file.readallbytes(@"c:\users\public\pictures\sample pictures\desert.jpg"));             filecontent.headers.contentdisposition = new contentdispositionheadervalue("attachment")             {                 filename = "desert.jpg"             };             content.add(filecontent, "file", "11");              foreach (var keyvaluepair in values)             {                 content.add(new stringcontent(keyvaluepair.value), keyvaluepair.key);             }              var response = client.postasync("exception/addexception", content).result;             var exceptionresult = response.content.readasasync<bool>().result;              return exceptionresult;         } 

the above code. not able read code service

i have no control on service code , cant change it

did try code @ post form data along files web api2. can merge both code blocks. works fine me.

web api code read files

using system.diagnostics; using system.net; using system.net.http; using system.threading.tasks; using system.web; using system.web.http;  public class uploadcontroller : apicontroller {     public async task<httpresponsemessage> postformdata()     {         // check if request contains multipart/form-data.         if (!request.content.ismimemultipartcontent())         {             throw new httpresponseexception(httpstatuscode.unsupportedmediatype);         }          string root = httpcontext.current.server.mappath("~/app_data");         var provider = new multipartformdatastreamprovider(root);          try         {             // read form data.             await request.content.readasmultipartasync(provider);              // illustrates how file names.             foreach (multipartfiledata file in provider.filedata)             {                 trace.writeline(file.headers.contentdisposition.filename);                 trace.writeline("server file path: " + file.localfilename);             }             return request.createresponse(httpstatuscode.ok);         }         catch (system.exception e)         {             return request.createerrorresponse(httpstatuscode.internalservererror, e);         }     }  } 

web api code read form data

public async task<httpresponsemessage> postformdata() {     if (!request.content.ismimemultipartcontent())     {         throw new httpresponseexception(httpstatuscode.unsupportedmediatype);     }      string root = httpcontext.current.server.mappath("~/app_data");     var provider = new multipartformdatastreamprovider(root);      try     {         await request.content.readasmultipartasync(provider);          // show key-value pairs.         foreach (var key in provider.formdata.allkeys)         {             foreach (var val in provider.formdata.getvalues(key))             {                 trace.writeline(string.format("{0}: {1}", key, val));             }         }          return request.createresponse(httpstatuscode.ok);     }     catch (system.exception e)     {         return request.createerrorresponse(httpstatuscode.internalservererror, e);     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -