如何通过URL将参数传递给Web服务

时间:2012-10-24 13:55:06

标签: c# web-services

我有可以成功使用的网络服务,但我正在与想要通过URL输入参数的其他人共享我的网络服务,例如://localhost:12345 / Lead.asmx?op = SendFiles& Id = 1234678& ;名称=乔&安培;姓=凯文

我补充说:

<webServices>
      <protocols>
        <add name="HttpGet"/>
      </protocols>
    </webServices>

到我的Web.Config文件,我的SendFile.asmx.cs代码如下所示:

    namespace SendFiles
   {
       /// <summary>
       /// Summary description for Service1
       /// </summary>
    [WebService(Namespace = "http://testco.co.za/")]
    [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 SendFile : System.Web.Services.WebService
    {

        [WebMethod]
        public bool PostToDB(LoadEntity _lead)
        {

            ConnectToSQLDB(ConfigurationManager.AppSettings["Server"],   ConfigurationManager.AppSettings["DB"],
                                ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["Password"], ref connectionRef);

            if (LI.ImportFiles(_lead, ref (error)) == true)
            {
                return true;
            }
            else
                return false;
        }

我尝试添加:

 [OperationContract]
    [WebGet]
    bool PostToDB(string IDNo, string FName, string SName);

但是我得到一个错误,我必须声明一个正文,因为它没有标记为抽象,外部或部分。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

回应您关于如何创建WCF休息服务的请求......

在您的服务合同中:

[ServiceContract]
public interface ITestService
{
    [WebGet(UriTemplate = "Tester")]
    [OperationContract]
    Stream Tester();
}

关于您的实施

public class TestService : ITestService
{
    public Stream Tester()
    {
        NameValueCollection queryStringCol = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters;

        if (queryStringCol != null && queryStringCol.Count > 0)
        {
            string parameters = string.Empty;
            for (int i = 0; i < queryStringCol.Count; i++)
            {
                parameters += queryStringCol[i] + "\n";
            }

            return new MemoryStream(Encoding.UTF8.GetBytes(parameters));
        }
        else
            return new MemoryStream(Encoding.UTF8.GetBytes("Hello Jersey!"));
    }
}

这只会打印出所有查询字符串值。您可以根据所获得的查询字符串参数执行您需要执行的任何处理。

例如,如果你放入。

http://localhost:6666/TestService/Tester?abc=123&bca=234

然后你会得到

123 234

作为你的输出。

如果您仍然需要,请参阅其余代码。这是使用控制台应用程序构建的,但它可以很容易地转换为Web。真正的重要内容是上面的内容。

class Program
{
    static ServiceHost _service = null;

    static void Main(string[] args)
    {
        _service = new ServiceHost(typeof(TestService));
        _service.Open();

        System.Console.WriteLine("TestService Started...");
        System.Console.WriteLine("Press ENTER to close service.");
        System.Console.ReadLine();

        _service.Close();
    }
}

<configuration>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
      <service name="ConsoleApplication1.TestService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6666/TestService"/>
          </baseAddresses>
        </host>
        <endpoint binding="webHttpBinding" contract="ConsoleApplication1.ITestService"
          behaviorConfiguration="webHttp"/>
      </service>      
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>          
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

答案 1 :(得分:0)

当您通过.asmx页面中的测试工具进行测试时,会生成什么URL?你可以把它交给你的来电者并验证他们执行你所做同一网址的能力吗?

如果使用非.NET客户端服务的其他人是您的主要用例,我建议使用基于WCF REST的服务。