c# - ASP.Net httpwebrequest not posting request body -


my hands in air - cry, uncle. me understand:

i wrote simple httpwebrequest posting clients' https://website. has specific header requirements , json request body. getresponsestream, after receiving getresponse statuscode.ok, error provided server. checked logs , said while headers present, request body ".

here code:

    public const string api_urilogin = "https://dev.clienturl.com/test/api/login";     private void requesttest()     {         loginapistruct logininfo = new loginapistruct();          logininfo.loginid = testid;         logininfo.password = testpw;         logininfo.vehicletype = "e";         logininfo.deviceid = "abcdef";         logininfo.clientid = "123456";         logininfo.appversion = string.empty;         logininfo.ostype = string.empty;         logininfo.osversion = string.empty;          // call loginapi         string loginstatus = loginapi(logininfo);         //display results         lblresults.text = loginstatus;     }       // public variables     public static string errormessage;      // ** client login **     public static string loginapi(loginapistruct logininfo)     {         // post login client.  response vehicle structure.           string logininfoserialized = string.empty;         string returnstring = string.empty;          logininfoserialized = new javascriptserializer().serialize(logininfo);          httpwebrequest request = (httpwebrequest)webrequest.create(api_urilogin);         request.method = "post";         request.contenttype = "application/json";         request.headers.add("from", "cc");         request.headers.add("language", "0");         request.headers.add("offset", "-8");         request.headers.add("to", "ccm");          // post request         using (streamwriter streampost = new streamwriter(request.getrequeststream()))         {             streampost.write(logininfoserialized);         }          // response         httpwebresponse response = (httpwebresponse)request.getresponse();          if (response.statuscode == httpstatuscode.ok)         {             // status okay.             //string responsestatusdescription = response.statusdescription;              using (var streamreader = new streamreader(response.getresponsestream()))             {                 try                 {                     var result = streamreader.readtoend();                      returnstring = result;                 }                 catch (exception ex)                 {                     returnstring = ex.message;                 }              }         }         else         {             returnstring = response.statuscode.tostring();         }          return returnstring;     }   // end loginapi 

my json string appears valid - paste fiddler, along setting headers, , fiddler returns successful login.

from asp.net, returns documented error email address (loginid) required. don't see incorrect in c# code. ideas else look? client have on hosting site blocks or filters out request? i'm stuck.

so, cares know.... must "close" streamwriter after performing .write(payload) command, otherwise doesn't send data.

    // post request         streamwriter streampost = new streamwriter(request.getrequeststream());         streampost.write(logininfoserialized);         streampost.close();    //don't forget close() or no data sent!! 

i hope helps else spend 2 weeks fighting omission. ciao.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -