c# - 400 error from webservice -
can explain me why im getting http 400 error when trying post webservice?
my service contract ::
[servicecontract] public interface ifldtwholesaleservice { [operationcontract] [webinvoke(method = "post", responseformat = webmessageformat.xml, bodystyle = webmessagebodystyle.wrapped, uritemplate = "mac")] string mac(string input);
my call;
private void posttowebsite() { httpwebrequest req = (httpwebrequest)httpwebrequest.create(txturl.text); req.method = "post"; req.mediatype = "text/xml"; string input = "dfwa"; req.contentlength = asciiencoding.utf8.getbytecount(input); streamwriter writer = new streamwriter(req.getrequeststream()); writer.write(input); writer.close(); var rsp = req.getresponse().getresponsestream(); txtout.text = new streamreader(rsp).readtoend(); }
my server config file
<system.servicemodel> <services> <service name="fldtrestwebservice.fldtwholesaleservice" behaviorconfiguration="httpbehaviour"> <endpoint address="" binding="webhttpbinding" contract="fldtrestwebservice.ifldtwholesaleservice" behaviorconfiguration="httpendpointbehavour"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange"/> <host> <baseaddresses> <add baseaddress="http://localhost:8080/contactservice/"/> </baseaddresses> </host> </service> </services> <behaviors> <servicebehaviors> <behavior name="httpbehaviour"> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="false"/> </behavior> </servicebehaviors> <endpointbehaviors> <behavior name="httpendpointbehavour"> <webhttp/> </behavior> </endpointbehaviors> </behaviors> </system.servicemodel>
edit :: gives same error when using mediatype "text/plain"
an endpoint webhttpbinding / webhttp behavior default accepts requests in either xml or json format. , xml send needs conform service expects. code below sends request service expects. also, notice need set contenttype property on httpwebrequest, not mediatype.
public class stackoverflow_6550019 { [servicecontract] public interface ifldtwholesaleservice { [operationcontract] [webinvoke(method = "post", responseformat = webmessageformat.xml, bodystyle = webmessagebodystyle.wrapped, uritemplate = "mac")] string mac(string input); } public class service : ifldtwholesaleservice { public string mac(string input) { return input; } } private static void posttowebsite(string url) { httpwebrequest req = (httpwebrequest)httpwebrequest.create(url); req.method = "post"; req.contenttype = "text/xml"; string input = @"<mac xmlns=""http://tempuri.org/""><input>hello</input></mac>"; streamwriter writer = new streamwriter(req.getrequeststream()); writer.write(input); writer.close(); var rsp = req.getresponse().getresponsestream(); console.writeline(new streamreader(rsp).readtoend()); } public static void test() { string baseaddress = "http://" + environment.machinename + ":8000/service/"; webservicehost host = new webservicehost(typeof(service), new uri(baseaddress)); host.open(); console.writeline("host opened"); // find out expected request, using wcf client. @ sends in fiddler var factory = new webchannelfactory<ifldtwholesaleservice>(new uri(baseaddress)); var proxy = factory.createchannel(); proxy.mac("hello world"); posttowebsite(baseaddress + "/mac"); console.write("press enter close host"); console.readline(); host.close(); } }
Comments
Post a Comment