asp.net web api - 404 NotFound with HttpClient -
i using httpclient class (from namespace system.net.http) post data asp.net web api action in windows form application. problem keep receiving status code 404 not found (endpoint not found). missing something? please help. thank in advance.
the following c# code uses httpclient make post request xml web api:
private async task postcustomform() { using (var client = new system.net.http.httpclient()) { nlog.logger logger = nlog.logmanager.getcurrentclasslogger(); try { customformlog customformlog = new customformlog(); customformlog.deviceid = 1234; customformlog.userexperienceid = 33442036854775807; customformlog.networkitemid = 58595555; client.baseaddress = new uri("http://xyzwebserver/"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new system.net.http.headers.mediatypewithqualityheadervalue("application/xml")); httpresponsemessage response = await client.postasxmlasync<customformlog>("logdata/customform/1234", customformlog); if (response.issuccessstatuscode) { logger.info(response.issuccessstatuscode); } else { logger.info(response); } } catch (httprequestexception ex) { logger.error(ex.message + " - " + ex.innerexception.message); messagebox.show(ex.message + " - " + ex.innerexception.message); } } }
and following help text xml web api web stie:
url: http://xyzwebserver/logdata/customform?device={deviceid} http method: post message direction format body request xml example,schema response n/a response body empty. following example request xml body: <customformlog> <optin>true</optin> <userexperienceid>3372036854775807</userexperienceid> <networkitemid>64093484</networkitemid> <formdata /> </customformlog> following request xml schema: <xs:schema elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="customformlog" nillable="true" type="customformlog" /> <xs:complextype name="customformlog"> <xs:sequence> <xs:element minoccurs="1" maxoccurs="1" name="optin" type="xs:boolean" /> <xs:element minoccurs="1" maxoccurs="1" name="userexperienceid" type="xs:long" /> <xs:element minoccurs="1" maxoccurs="1" name="networkitemid" type="xs:int" /> <xs:element minoccurs="0" maxoccurs="1" name="formdata"> <xs:complextype> <xs:sequence> <xs:any processcontents="lax" /> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:schema>
finally, custom dto class:
public class customformlog { public int deviceid { get; set; } public bool optin { get;set;} public long userexperienceid { get; set; } public int networkitemid { get; set; } public xelement formdata { get; set; } }
ok information seems url expcted
url: http://xyzwebserver/logdata/customform?device={deviceid}
but sending
url: http://xyzwebserver/logdata/customform/1234
this wont turn correct url until web api route config has {device} defined in route config. suggest use mentioned format url , change requesting line to
httpresponsemessage response = await client.postasxmlasync<customformlog>("logdata/customform?device=1234", customformlog);
Comments
Post a Comment