Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I am new to consume a Web service through HttpWebRequest.So i create a dummy application for learning purpose.My question may be silly but answers would be appreciated.

I have created a demo web service, Below is the code
C#
[WebMethod]
        public DataSet GetEmployees()
        {
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["FAST"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from Employee", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }


Here i am returning DataSet and i want to bind it in GridView in my client Application.
The Below are the Client application code
C#
string url = "http://localhost:2018/EmployeeService.asmx";
            HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url);
            webreq.ContentType = "text/xml; charset=utf-8";
            //webreq.Accept = "text/xml";
            webreq.Headers.Clear();
            webreq.Method = "POST";
            Encoding encode = Encoding.GetEncoding("utf-8");
            HttpWebResponse webres = null;
            webres = (HttpWebResponse)webreq.GetResponse();
            Stream reader = null;
            reader = webres.GetResponseStream();
            StreamReader sreader = new StreamReader(reader, encode, true);
            string result = sreader.ReadToEnd();


While running i am getting error "The remote server returned an error: (500) Internal Server Error." in this line, webres = (HttpWebResponse)webreq.GetResponse();

Anyone have any idea where i did mistake.
Posted

1 solution

I think you need to pass the method name also in the url ...
here is a sample
Simple And Generic Web Service Proxy Using HttpWebRequest/HttpWebResponse objects [^]

Your URL should be
C#
string url = "http://localhost:2018/EmployeeService.asmx/GetEmployees";
 
Share this answer
 
v2
Comments
Ashad25 16-Jul-13 5:58am    
Hey Thanks, i got the result by simply adding the method name to my service url.
I have one doubt,suppose in Webservice i have more than one methods,so in this case how to call the webservice.May be we have to call the webservice without mentioning the methodnames.
Naz_Firdouse 16-Jul-13 6:29am    
yeah thats what I mentioned above that you need to append method name to the URL.
In case of multiple methods also, I think you need to create another HTTPWebRequest.
See this
request a 2nd url with the same httpwebrequest obj[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900