Java POST request to ASP.NET fails, C# works -
i have asp.net web api2 application controller has:
public class licensecontroller : apicontroller { [route("license")] [httppost] public xmldocument postlicense([frombody] xmldocument inputstream) { xmldocument doc = new xmldocument(); doc.loadxml("<root/>"); return doc; } }
and if post c# app, receives request , returns "root/>":
static void main(string[] args) { string xml = "<david><shirley/></david>"; uri httpsite = new uri(phonehomeuri + "/license"); webrequest wreq = webrequest.create(httpsite); wreq.usedefaultcredentials = true; wreq.preauthenticate = true; wreq.credentials = credentialcache.defaultcredentials; wreq.method = "post"; wreq.contenttype = "text/xml"; stream request = wreq.getrequeststream(); byte[] bytearray = encoding.utf8.getbytes(xml); request.write(bytearray, 0, bytearray.length); httpwebresponse response = (httpwebresponse) wreq.getresponse(); stream receivestream = response.getresponsestream(); encoding encode = system.text.encoding.getencoding("utf-8"); streamreader readstream = new streamreader(receivestream, encode); string s = readstream.readtoend(); }
but when try same thing in java (below), while "" returns correctly, inputstream parameter in "[frombody] xmldocument inputstream" null.
httpurlconnection urlconnection = (httpurlconnection) new url(phonehomeuri + "/license").openconnection(); urlconnection.setdooutput(true); urlconnection.setdoinput(true); urlconnection.setchunkedstreamingmode(0); urlconnection.setrequestmethod("post"); urlconnection.setrequestproperty("accept-charset", "utf-8"); urlconnection.setrequestproperty("content-type", "text/xml"); outputstreamwriter writer = new outputstreamwriter(urlconnection.getoutputstream(), "utf-8"); writer.write(xml); writer.flush(); inputstream result = urlconnection.getinputstream();
i've tried changing lots of settings on java code makes call. why not getting xml posted web app controller?
thanks - dave
Comments
Post a Comment