如何使用HttpWebRequest来使用WebService

如何使用HttpWebRequest来使用WebService

本文介绍了如何使用HttpWebRequest来使用WebService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是新手通过HttpWebRequest使用Web服务。所以我创建了一个用于学习目的的虚拟应用程序。我的问题可能很愚蠢但是答案我将不胜感激。



我创建了一个演示网络服务,下面是代码

 [WebMethod ] 
public DataSet GetEmployees()
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings [ FAST]。ConnectionString);
SqlCommand cmd = new SqlCommand( select *来自员工,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}





这里我返回DataSet,我想在我的客户端应用程序中将它绑定在GridView中。

以下是客户应用程序代码

  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;
编码encode = Encoding.GetEncoding( utf-8);
HttpWebResponse webres = null ;
webres =(HttpWebResponse)webreq.GetResponse();
流阅读器= null ;
reader = webres.GetResponseStream();
StreamReader sreader = new StreamReader(reader,encode, true );
string result = sreader.ReadToEnd();





运行时我收到错误远程服务器返回错误:(500)内部服务器错误。在此行中,webres =(HttpWebResponse)webreq.GetResponse();



任何人都知道我错在哪里。

解决方案

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

[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

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.

解决方案


这篇关于如何使用HttpWebRequest来使用WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:11