api - C# POST request to webapi using a console program -
i have web api can access through browser :-
i trying create simple console program in c# using vs2015 send data , receive response using http post.
here have far:-
using system; using system.net.http; using system.net.http.headers; using system.threading.tasks; namespace websample { class apisenddata { public string apifunction { get; set; } public string dppname { get; set; } public string cleardata { get; set; } public string dppversion { get; set; } } class program { static void main(string[] args) { // main function calls async method named runasync // , blocks until runasyncc completes. runasync().wait(); } static async task runasync() { using (var client = new httpclient()) { // code sets base uri http requests, // , sets accept header "application/json", // tells server send data in json format. client.baseaddress = new uri("https://localhost:8443/ncrapi"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); // http post var datatobesent = new apisenddata() { apifunction ="ncrsecuredata", dppname ="csamplecustomer", dppversion ="latest", cleardata ="1234567890" }; httpresponsemessage response = await client.postasjsonasync("ncrapi", datatobesent); if (response.issuccessstatuscode) { // uri of created resource. uri ncrurl = response.headers.location; // whatever need here returned data // } } } } }
however getting error on httpresonsemessage response statement....{"an error occurred while sending request."} {"the request aborted: not create ssl/tls secure channel."}
i suspect because not correctly understanding the client.baseaddress , httpresponsemessage response statements.
here have been following:- http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
you getting error because final address baseaddress + post address, is: http://localhost:8443/nrcapi/nrcapi , doesn't exist
try changing client.baseaddress to:
client.baseaddress = new uri("https://localhost:8443/");
for ssl connection errors, try generating trusted certificate: make https call using httpclient
Comments
Post a Comment