Sunday, 15 February 2015

c# - REST Service Getting 400 bad request -



c# - REST Service Getting 400 bad request -

i have created restful web service in c#. getting 400 bad request error when phone call post method. checking requests in fiddler , post requests fine. don't understand wrong program.

here of code snippets. if of need other code have @ please ask.

interface

[servicecontract] public interface iread { [operationcontract] [webinvoke(uritemplate = "getcard", method = "post")] someobject getcard(session session); }

the nfcsession object has int variable of name session.

client generating post request

public void getcard() { string strgetcard = "http://localhost:8384/reader/getcard"; byte[] databyte = generatenfcsession(63315152); httpwebrequest postrequest = (httpwebrequest)webrequest.create(strgetcard); postrequest.method = "post"; postrequest.contenttype = "application/xml;charset=utf-8"; postrequest.contentlength = databyte.length; stream poststream = postrequest.getrequeststream(); poststream.write(databyte, 0, databyte.length); httpwebresponse postresponse = (httpwebresponse)postrequest.getresponse(); streamreader reader = new streamreader(postresponse.getresponsestream(), encoding.utf8); console.writeline("response"); console.writeline(reader.readtoend().tostring()); }

xml generator

private static byte[] generatexml(int sessionid) { memorystream mstream = new memorystream(); xmltextwriter xmlwriter = new xmltextwriter(mstream, encoding.utf8); xmlwriter.formatting = formatting.indented; xmlwriter.writestartdocument(); xmlwriter.writestartelement("session"); xmlwriter.writestartelement("session"); xmlwriter.writestring(sessionid.tostring()); xmlwriter.writeendelement(); xmlwriter.writeendelement(); xmlwriter.writeenddocument(); xmlwriter.flush(); xmlwriter.close(); console.writeline(mstream.toarray().tostring()); homecoming mstream.toarray(); }

web.config

<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetframework="4.0" /> </system.web> <system.webserver> <modules runallmanagedmodulesforallrequests="true"> <add name="urlroutingmodule" type="system.web.routing.urlroutingmodule, system.web,version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a" /> </modules> </system.webserver> <system.servicemodel> <behaviors> <servicebehaviors> <behavior> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="false"/> </behavior> </servicebehaviors> </behaviors> <servicehostingenvironment aspnetcompatibilityenabled="true"/> <standardendpoints> <webhttpendpoint> <standardendpoint name="" helpenabled="true" faultexceptionenabled="true" automaticformatselectionenabled="true"></standardendpoint> </webhttpendpoint> </standardendpoints> </system.servicemodel> <system.webserver> <modules runallmanagedmodulesforallrequests="true"/> </system.webserver> </configuration>

post request xml fiddler

<?xml version="1.0" encoding="utf-8"?> <session> <session>63315152</session> </session>

i error

the server encountered error processing request.

note: when provide xmlserializer format attribute works fine. want service work json requests @ later point. , point of view not necessary explicitly provide format attribute. don't understand wrong code.

guys please help!!!

try adding <enablewebscript /> within behavior tag:

... <behaviors> <servicebehaviors> <behavior> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="false"/> <enablewebscript /> </behavior> </servicebehaviors> </behaviors> ...

it's discussed here: http://social.msdn.microsoft.com/forums/en/wcf/thread/fb48999f-cd41-49b8-ae9a-f0c8f5aebbd2

c# .net visual-studio-2010 wcf rest

No comments:

Post a Comment