c# - 通过在 Web 应用程序表单中返回一串数据来测试 Web 服务

标签 c# asp.net .net web-services

我有一个 Web 服务,但需要知道如何在 Windows 应用程序表单中测试它。

这是服务的开始。我会将表单代码放在按钮中,还是将其返回到标签中?不太了解 c# 或 .net 我已经成功调用了 Web 服务,但只需要返回字符串以确保加密有效。

<%@ WebService Language="C#" Class="UserEncryptionLink.EncryptUserLink" %>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Text;
using System.Security.Cryptography;




namespace UserEncryptionLink
{
/// <summary>
   /// This Web Service is to encrypt user details and display them in the URL when  they   click on a link taking them to InfoExchange
/// </summary>
[WebService(Namespace = " Webspace name")]
[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 EncryptUserLink : System.Web.Services.WebService
{
[WebMethod]
public void TestCypher()
{
    var key = "12345";
    var vector = "12345";
    var username = "YOURDOMAIN\\YOURUSERNAME";
    var url = "sitename.com";
    var it = GetSingleSignOnUrl(url, username, key, vector);
}

还有我的表格

是的,我有引用该服务的表格,它看起来像这样。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FormForEncrypt.nameofURL;
using System.Net;



namespace FormForEncrypt
{


public partial class EncryptForm : Form
{


    public EncryptForm()
    {
        InitializeComponent();


    }

    // creates an instance of the web service as a member of the class, will put it down below, doesn't seem to work though declared in the using statement
    private nameofURL.EncryptUserLink userform = new nameofUrl.EncryptUserLink();



    [System.Web.Services.WebMethod]

    private void testButton_Click_1(object sender, EventArgs e)

    {
        //method to send on button click, then recieve the string to show it works, it should come out as http://CLIENTNAME.info-exchange.com/yyyyMMddHHmmssDomainUsername 

        //create instances of the details


        string[] it;

        //send string

        //recieve string and display, it must display the same as what was sent. 


    }
}

最佳答案

如果您应该将表单代码放在按钮内或将其返回到标签中,您不确定您的意思是什么? Web 服务没有表单或标签或任何东西的概念。它们只是服务……没有表单,没有控件……只是有点“原始代码”等待通过调用它来提供服务,您的托管协议(protocol)(例如 IIS 或 WAS 或自托管应用程序)负责将其启动并运行该服务。

调用它的方式...好吧,这取决于您要从应用程序中调用它的位置。可能是点击按钮,可能是计时器,可能是表单加载...取决于您和您的应用程序逻辑。

您当前的 Web 方法 TestChypher 目前不返回任何内容。我猜你需要返回一个 bool 值来指示字符串是否匹配?

同样,取决于您和您想要返回的内容。如果您想返回一个字符串,请更改方法签名以返回一个字符串 - 因此将“void”更改为“string”并在该方法中返回您想要返回的字符串。

示例 - 短片:

[WebMethod]
public void SayHello()
{
   return "Hello";
}

关于c# - 通过在 Web 应用程序表单中返回一串数据来测试 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19952760/

相关文章:

c# - C# 中的 Func<TResult> 是什么?

c# - LINQ:获取表详细信息

c# - 获取接口(interface)但不是基础接口(interface)

c# - 为 ASP.NET MVC5 关系创建 DropDownList

c# - 强制 .NET 编写原生波斯数字而不是美国格式

c# - 序列包含多个匹配元素 - 使用 Entity Framework 添加项目

c# - 如何使用 mvc 4 web api 中的 HttpResponseMessage 对象设置 etag?

c# - JSON到ObservableCollection以在ListView中使用

c# - 如何获取Listbox控件的选中项?

c# - 带有 .Years 和 .Months 的实时时间跨度对象