【问题标题】:Testing a web service by returning a string of data in a web application form通过在 Web 应用程序表单中返回一串数据来测试 Web 服务
【发布时间】:2013-11-13 11:36:14
【问题描述】:

我有一个 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 服务是否正常工作,请在本地浏览它 - 您应该能够看到并调用每个方法。至于您将代码放在表单中的哪个位置 - 如果它不是微不足道的,它应该在业务/数据访问/服务层中 - 否则按钮处理程序很好

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


【解决方案1】:

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

调用它的术语...嗯,这取决于您想从应用程序中调用它的位置。可能是单击按钮,可能是计时器,可能是表单加载...取决于您和您的应用程序逻辑。

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

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

示例 - 短片:

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

【讨论】:

  • 我的意思是我创建了一个调用服务的 Web 表单,我现在想返回字段以确保服务正在加密并显示加密的 URL,我真的不知道如何进行此操作,我将如何获取我创建的调用 Web 服务以返回我想要的 URL 字符串的 Web 表单?
  • 按照我的例子,它直截了当。您是否设法通过单击按钮从您的网络表单中调用您的网络服务?如果没有,您需要在项目中添加对 web 服务的引用,然后您应该能够访问它,因为可用的方法将被公开并且您可以调用它。此外,更新您的问题标签以包含 ASP.NET。
  • 所以你没有调用服务的代码?只需单击即可执行 userform.TestCypher() 。这将调用服务(但不会返回任何作为它的 void 方法)
  • 是的,有人告诉我只返回一个字符串来表明加密正在工作,我不知道该怎么做。它是我认为的实际 URL 之后的加密 URL。
  • 您是否阅读了我关于如何修改方法以返回某些内容的答案? :) 将“void”更改为“string”并返回您想要从 web 服务返回的字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
相关资源
最近更新 更多