c# - Consuming java web service getting error -
i getting error while consuming java web service in win form application
the error
you must provide request body if set contentlength>0 or sendchunked==true. calling [begin]getrequeststream before [begin]getresponse.
my code consume java service
public byte[] stringtobytearray(string stringdata) { system.text.utf8encoding encoding = new system.text.utf8encoding(); return encoding.getbytes(stringdata); } private void button1_click(object sender, eventargs e) { string data = @"<repositorytype>117</repositorytype> <repositorycategory>0</repositorycategory> <modifiedby>2825</modifiedby> <referencecode>0</referencecode> <fromdate>2015-10-14t11:50:00</fromdate> <todate>2015-10-14t11:51:00</todate> <repositoryname>ashok</repositoryname> <repositoryshortname>kumar</repositoryshortname> <repositorydesc>nothing</repositorydesc> <fixed>f</fixed> <isvalid>true</isvalid> <lstvisa /> <sortorder>0</sortorder> </repository>";` byte[] postdata = null; httpwebrequest _webrequest = null; httpwebresponse webresponse = null; streamreader responsestream = null; string sreturnval = string.empty; string serviceaddress="http://172.16.12.21:8888/xml_response/rest/test/xmltest/"; try { _webrequest = (httpwebrequest)webrequest.create(serviceaddress + "/" + data); postdata = stringtobytearray(data); if (_webrequest != null) { if (postdata!=null) { _webrequest.method = "post"; _webrequest.contenttype= "text/xml"; _webrequest.contentlength = postdata.length; _webrequest.useragent = "mozilla/4.0 (compatible; msie 7.0; windows nt 5.1)"; _webrequest.sendchunked = true; } **webresponse = (httpwebresponse)_webrequest.getresponse();** { if (webresponse.headers.get("content-encoding") != null && webresponse.headers.get("content-encoding").tolower() == "gzip") responsestream = new streamreader(new gzipstream(webresponse.getresponsestream(), compressionmode.decompress)); else { encoding enc = system.text.encoding.getencoding(1252); responsestream = new streamreader(webresponse.getresponsestream(), enc); } if (responsestream != null) { xelement root = xelement.load(responsestream); sreturnval = root.value; } } } else { throw new exception("connection " + " service not established.", new exception("please check whether " + " service running or contact system administrator.")); } } catch(exception ex) { } }
the highlighted line getting error.
please in this.
you're adding data url instead of posting request body. take @ question working code can use: http post using web service. asp.net webservices must set soapaction http header, can skip line if service doesn't require it.
Comments
Post a Comment