无法在GET中发送GET

时间:2019-04-04 09:45:01

标签: c# rest windows-services

我正在尝试将REST代理构建为Windows服务。但是,每当我的程序执行GET时,我都会收到“不允许使用405方法”

结构:

  1. 代码1-获取->代码2
  2. 浏览器--GET->代码1 --GET->代码2

在第一种情况下,我的代码只是对另一个REST服务器进行GET调用。一切正常。

在第二种情况下,我从浏览器向第一个客户端进行GET调用,它将作为代理工作,将GET调用重定向到第二个客户端。这行不通。 POST有效,只有GET不起作用。

我尝试了不同的URL路径和其他URL结构。

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

namespace RUST_Console_Write
{
    [ServiceContract]
    public interface IService // <-- interface of second REST client
    {
        [OperationContract]
        [WebGet(UriTemplate = "Test1/{s}")]
        string Test1(string s);

        [OperationContract]
        [WebInvoke]
        string EchoWithPost(string s);
    }

    [ServiceContract]
    public interface IMITM_Service
    {
        [OperationContract]
        [WebGet(UriTemplate = "GET1?s={s}")]
        string EchoGet1(string s);

        [OperationContract]
        [WebInvoke]
        string EchoWithPost(string s);
    }

    public class REST_CALLS
    {
        public static string makeGET(string s)
        {
            string messageResponse = "Response from other REST Service:\n";

            try
            {
                using (ChannelFactory<IService> channelFactory = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8001"))
                {
                    channelFactory.Endpoint.EndpointBehaviors.Add(new WebHttpBehavior());
                    IService channel = channelFactory.CreateChannel();

                    Console.Write("Sending the GET... ");
                    messageResponse += channel.Test1(s);
                    Console.WriteLine("Success");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed");
                Console.WriteLine(e);
            }

            return messageResponse;
        }

        public static string makePOST(string s)
        {
            string messageResponse = "Response from other REST Service:\n";

            try
            {
                using (ChannelFactory<IService> channelFactory = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8001"))
                {
                    channelFactory.Endpoint.EndpointBehaviors.Add(new WebHttpBehavior());
                    IService channel = channelFactory.CreateChannel();

                    Console.Write("Sending the POST... ");
                    messageResponse += channel.EchoWithPost(s);
                    Console.WriteLine("Success");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed");
                Console.WriteLine(e);
            }

            return messageResponse;
        }
    }

    public class MITM_Service : IMITM_Service
    {
        public string EchoGet1(string s)
        { 
            return REST_CALLS.makeGET(s); // <---- this one fails (405)
        }

        public string EchoWithPost(string s)
        {
            return REST_CALLS.makePOST(s); // <---- this one works fine
        }
    }

    class Program
    {
        static void Main()
        {
            WebServiceHost host = new WebServiceHost(typeof(MITM_Service), new Uri("http://localhost:8000"));
            ServiceEndpoint serviceEndpoint = host.AddServiceEndpoint(typeof(IMITM_Service), new WebHttpBinding(), string.Empty);
            host.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageEnabled = false;
            host.Open();

            Console.WriteLine("First call");
            Console.WriteLine(REST_CALLS.makeGET("TEST")); // <---- this one works fine

            Console.WriteLine("\nSecond call");
            Console.WriteLine(REST_CALLS.makePOST("TEST")); // <---- this one works fine

            Console.ReadLine();

            host.Close();
        }
    }
}

Id非常感谢您的帮助。不能为我的生活弄清楚。

(我知道这还不是Windows服务,错误是相同的,可以像正常程序一样进行调试)

0 个答案:

没有答案