如何创建使用WebRequest / Response数据的函数 [英] How to Create a function that consumes data from WebRequest/Response

查看:61
本文介绍了如何创建使用WebRequest / Response数据的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在网上阅读了一些关于如何使用REST功能的文章。现在我正在尝试创建一个将从webrequest / response中使用xml的函数。



我知道如何使用这样的功能但不知道如何创建一个。

请告诉我如何创建这样的功能



我想从客户端发送webrequest中的xml。我需要创建一个将从服务器上的客户端使用此xml的函数。请帮助..

解决方案

您可以像这样创建webservice(.asmx)





  ///   <  摘要 >  
/// GetDataWebService的摘要说明
/// < / summary >
[WebService(Namespace = http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem( FALS e )]
// 允许从脚本调用此Web服务,使用ASP .NET AJAX,取消注释以下行。
// [System.Web.Script。 Services.ScriptService]
public class GetDataWebService:System.Web.Services。 WebService
{

BooksDBEntities db = new BooksDBEntities();


[WebMethod]
public string HelloWorld ()
{
return Hello World;
}

[WebMethod]
public 列表< studentserialized> GetAllStudentData()
{
List< student> lststu = new 列表< student>();
lststu = db.Students.ToList();
列表< studentserialized> lstss = new 列表< studentserialized>();
foreach (学生sss in lststu)
{
StudentSerialized ss = new StudentSerialized();

ss.ID = sss.ID;
ss.Name = sss.Name;
ss.Roll = sss.Roll;

lstss.Add(ss);
}

return lstss;

}

public class StudentSerialized
{
public int ID { get ; set ; }
public string 名称{ get ; set ; }
public string Roll { get ; set ; }


}


}



此函数(GetAllStudentData)将返回XML。 



 <   arrayofstudentserialized     xmlns:xsi   =   http://www.w3.org/2001/XMLSchema-instance   < span class =code-attribute> xmlns:xsd   =  http://www.w3。 org / 2001 / XMLSchema    xmlns   =   http://tempuri.org/ >  
< studentserialized >
< span class =code-keyword>< id > 1 < / id >
< 名称 > Anis < span class =code-keyword>< / name >
< roll > 0718004 < span class =code-keyword>< / roll >
< / studentserialized >
< studentserialized >
< id > 2 < / id >
< name > Toma < / name >
< roll > 0718040 < / roll >
< / studentserialized >
< studentserialized >
< id > 3 < / id >
< 名称 > Bachchu < / name >
< roll > 0717007 < / roll >
< / studentserialized >
< / arrayofstudentserialized &g吨;


Hello ,

I read some articles on web about how to consume a REST function. Now I am trying to create a function that will consume xml from webrequest/response.

I know how to consume such a function but don't know how to create one.
Please tell me how to create such a function

I want to send xml in webrequest from client. I need to create a function that will consume this xml from client at server. Please help ..

解决方案

You can create webservice(.asmx) like this one



/// <summary>
/// Summary description for GetDataWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class GetDataWebService : System.Web.Services.WebService
{

    BooksDBEntities db = new BooksDBEntities();


    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public List<studentserialized> GetAllStudentData()
    {
        List<student> lststu = new List<student>();
        lststu = db.Students.ToList();
        List<studentserialized> lstss = new List<studentserialized>();
        foreach (Student sss in lststu)
        {
            StudentSerialized ss = new StudentSerialized();

            ss.ID = sss.ID;
            ss.Name = sss.Name;
            ss.Roll = sss.Roll;

            lstss.Add(ss);
        }

        return lstss;

    }

  public  class StudentSerialized
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Roll { get; set; }


    }


}


This function(GetAllStudentData)  will return an XML.


<arrayofstudentserialized xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <studentserialized>
    <id>1</id>
    <name>Anis</name>
    <roll>0718004</roll>
  </studentserialized>
  <studentserialized>
    <id>2</id>
    <name>Toma</name>
    <roll>0718040</roll>
  </studentserialized>
  <studentserialized>
    <id>3</id>
    <name>Bachchu</name>
    <roll>0717007</roll>
  </studentserialized>
</arrayofstudentserialized>


这篇关于如何创建使用WebRequest / Response数据的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆