C#如何从asmx Web Service获取/设置数据

时间:2016-07-28 02:42:54

标签: c# web-services soap asmx

我正在使用VisualStudio2013。重要的是要注意读者,这个asmx源自的代码工作得很好,但我不知道如何使用asmx WebService。我从这里下载了整整九码https://sourceforge.net/projects/shorturl-dotnet/

我无法弄清楚如何获取/设置以下CreateUrl()WebMethod的属性。我想学习如何使用整个WebService,但是从这里开始。

在下面的示例中,我将URL发送到CreateURL()方法,该方法将缩短URL并执行其他任务;我不知道如何从返回的ShortUrl.Container类中获取属性:在将类返回到我的调用方法后,我还没有成功访问数据。

// WebMethod

public class API  : System.Web.Services.WebService {

[WebMethod]
public ShortUrl.Container CreateUrl(string real_url)
{
    ShortUrl.Container oShortUrl = new ShortUrl.Container();

    oShortUrl.RealUrl = real_url;
    oShortUrl.ShortenedUrl = ShortUrl.Utils.UniqueShortUrl();
    oShortUrl.CreateDate = DateTime.Now;
    oShortUrl.CreatedBy = HttpContext.Current.Request.UserHostAddress;

    ShortUrl.Utils.AddUrlToDatabase(oShortUrl);

    oShortUrl.ShortenedUrl = ShortUrl.Utils.PublicShortUrl(oShortUrl.ShortenedUrl);

    return oShortUrl;
    }
}

// ShortUrl.Container类以oShortUrl

的形式返回
namespace ShortUrl
{
    /// <summary>
    /// Container for the ShortURL object
    /// </summary>
    public class Container
    {
        private string _real_url;
        private string _short_url;
        private DateTime _create_date;
        private string _created_by;

        public Container()
        {
            this.CreateDate = DateTime.Now;
            this.CreatedBy = "tap";
            this.RealUrl = null;
            this.ShortenedUrl = "Unknown";
        }

        public string RealUrl
        {
            get { return _real_url;  }
            set { _real_url = value; }
        }

        public string ShortenedUrl
        {
            get { return _short_url;  }
            set { _short_url = value; }
        }

        public DateTime CreateDate
        {
            get { return _create_date;  }
            set { _create_date = value; }
        }

        public string CreatedBy
        {
            get { return _created_by;  }
            set { _created_by = value; }
        }
    }
}

在VS2013中,我添加了服务引用以指向http://tap.tools.api.asmx作为服务端点,并将VS2013引用命名为ShortenUrl。 VS2013生成APISoapClient和Container类。

// get/set properties of the ShortUrl.Container class
// by means of APISoapClient
ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient();
u.CreateUrl("http://clintongallagher.com/tag-target-url.html");


// get/set properties of the ShortUrl.Container class
// by means of Container class
ShortenUrl.Container c = new ShortenUrl.Container();
string url = c.RealUrl;

我没有随处可见,我认为我的问题是在公共ShortUrl.Container CreateUrl(string real_url)方法中实例化的oShortUrl对象的实例。我不知道如何从oShortUrl的实例中获取任何属性,Container类返回到我的方法。

// oShortUrl 
ShortUrl.Container oShortUrl = new ShortUrl.Container();

奇怪,因为它可能听起来像陈旧和过时,asmx的使用恰好是我从未与-any- WebServices合作,这解释了为什么我很弱并且让自己受到法院的怜悯。

//编辑:2016-07-19~2:41

VS2013从WSDL生成了几个类,其中两个看起来很有用,如Intellisense中所见......

//类APISoapClient和类Container

当我使用API​​SoapClient的局部变量时,会生成缩短的URL,因为我可以看到使用SQL Management Studio并注意所有数据都已正确生成但我无法获取/设置任何其他WebMethods或属性获取/设置数据......

// Exposes two WebMethods: CreateUrl and GetUrl    
ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient();  

// Does generate the shortened URL
u.CreateUrl("http://clintongallagher.com/tag-target-url.html");
// Should return the URL that was shortened but doesn't
u.GetUrl("i2Z5H");

和...

// Exposes the properties in Intellisense but does not return data
ShortenUrl.Container c = new ShortenUrl.Container();

// returns 1/1/0001 12:00:00 AM 
lblCreateDate.Text = "CreateDate: " + c.CreateDate.ToString();
// returns nothing
lblCreatedBy.Text = "CreatedBy: " + c.CreatedBy;
// returns nothing
lblRealUrl.Text = "RealUrl: " + c.RealUrl;
// returns ShortenUrl.Container
lblShortenedUrl.Text = "ShortenedUrl: " + u.GetUrl("i2Z5H"); 

2 个答案:

答案 0 :(得分:1)

如果我理解你想要获得的是从Web方法返回的容器。如果是这样,那么只需创建一个变量类型的Container并为其分配方法调用。与ShortUrl.Container c = u.CreateUrl(...)一样,您可以从c获取您正在寻找的值。

答案 1 :(得分:1)

想想这个@clintongallagher。当您进行以下呼叫时,

ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient();
u.CreateUrl("http://clintongallagher.com/tag-target-url.html");


[WebMethod]
public ShortUrl.Container CreateUrl(string real_url)
{
    ShortUrl.Container oShortUrl = new ShortUrl.Container();

    oShortUrl.RealUrl = real_url;

    //here you're assigning a value to this object, let's say 'A'
    oShortUrl.ShortenedUrl = ShortUrl.Utils.UniqueShortUrl(); 

    oShortUrl.CreateDate = DateTime.Now;
    oShortUrl.CreatedBy = HttpContext.Current.Request.UserHostAddress;

    //then here you're saving the object with the Shortened value 'A' you just got
    ShortUrl.Utils.AddUrlToDatabase(oShortUrl); 

    /*
     *finally you're replacing the Shortened value with another value, 
     *let's say 'B', which is the object you're going to return*/
    oShortUrl.ShortenedUrl = ShortUrl.Utils.PublicShortUrl(oShortUrl.ShortenedUrl);

    return oShortUrl;
}

我不知道GetUrl(shortened_value)应该如何工作但是,假设它将从DB获取传入的shortened_value,当然结果将不相同,因为保存的缩短值是'A '和你要求B