Using System.Net classes to dynamically invoke web-service method
please refer question posted http://forums.asp.net/t/1699089.aspx/1?using+system+net+classes+to+dynamically+invoke+web+service+method
stating code blocks here too..
here's code of web service..
using system.web; using system.web.services; using system.data; [webservice(namespace = "http://tempuri.org/")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] [system.web.script.services.scriptservice] public class service : system.web.services.webservice { [webmethod] public dataset getdata(string strcode, string strkey) { dataset dstdata = null; tbldata objdata = null; objdata = new tbldata(); dstdata = objdata.getrecords(strcode); return dstdata; } }
here's code of aspx.cs..
private dataset createwebrequest() { byte[] postbytes; httpwebrequest request; stringbuilder poststring = new stringbuilder(); poststring.append( string.format( "strcode={0}&strkey={1}", "value1", "value2") ); postbytes = encoding.utf8.getbytes(poststring.tostring()); string url = "http://localhost/mywebservices/service.asmx"; //string action = url + "/getdata"; // action line 1 string action = "http://tempuri.org/getdata"; // action line 2 request = (httpwebrequest)webrequest.create(url); request.method = "post"; // or "get" if want make request request.contentlength = postbytes.length; //request.contenttype = "application/x-www-form-urlencoded"; //request.contenttype = "application/xml; charset=utf-8"; request.contenttype = "text/plain"; request.headers.add("soapaction", action); stream requeststream = request.getrequeststream(); streamwriter writer = new streamwriter(requeststream); writer.write(poststring.tostring()); writer.close(); httpwebresponse response = (httpwebresponse)request.getresponse(); streamreader reader = new streamreader(response.getresponsestream(), encoding.utf8); string xml = reader.readtoend(); dataset dstdata = new dataset(); dstdata.readxml(reader); return dstdata; }
please let me know solution.
thanks..
i'm trying following code, still getting run-time error @ line calling getresponse().
private dataset callwebservice() { httpwebrequest request; string url = "http://localhost/mywebservices/service.asmx"; //string action = "/getalldata"; string action = "?op=getalldata"; //request = (httpwebrequest)webrequest.create(url); request = (httpwebrequest)webrequest.create(url + action); request.method = "post"; //request.contenttype = "text/xml; charset=utf-8"; //request.headers.add("soapaction", action); httpwebresponse response = (httpwebresponse)request.getresponse(); //hangs while here , getting error "the operation has timed out" streamreader reader = new streamreader(response.getresponsestream(), encoding.utf8); string xml = reader.readtoend(); dataset dstdata = new dataset(); dstdata.readxml(reader); return dstdata; }
Archived Forums A-B > ASMX Web Services and XML Serialization
Comments
Post a Comment