Wcf Restful service post username with parameter

时间:2016-09-06 09:51:38

标签: c# wcf http-post restful-authentication wcf-rest

我是开发人员 C#.Net 我正在为我的服务器编写 Wcf restful service 以进行 mobile 通信。但使用带参数的 post方法发送 json请求时出现问题。从客户端 发送请求后,服务器显示错误: 400错误请求 ...

我的服务合同

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json, UriTemplate = "login", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string login(string username, string password);

实施服务:

public string login(string username, string password)
{
   string str = String.Empty;
   if (username == "admin" && password == "admin")
       str = "Success";
   else
       str = "Invalid";
   return str;
}

修改

我的主人是:

public void Start()
        {
            Stop();

            //string strAdrHTTP = "http://localhost:" + HttpPort + "/WcfMobileService";
            string strAdrHTTP = "http://192.168.1.190:" + HttpPort + "/WcfMobileService";
            Uri[] adrbase = { new Uri(strAdrHTTP) };

            m_svcHost = new ServiceHost(typeof(TWcfMobileService), adrbase);
            WebServiceHost webServiceHost =
                new WebServiceHost(typeof(TWcfMobileService), adrbase);


            ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
            m_svcHost.Description.Behaviors.Add(mBehave);

            mBehave.HttpsGetEnabled = true;
            mBehave.HttpGetEnabled = true;

            WebHttpBinding webBinding = new WebHttpBinding();
            webServiceHost.AddServiceEndpoint(typeof(IWcfMobileServiceContract), webBinding, "rest");

            WebHttpBinding restBinding = new WebHttpBinding();

            ServiceEndpoint restSEP =
                m_svcHost.AddServiceEndpoint(typeof(IWcfMobileServiceContract),
                                               restBinding, "rest");
            restSEP.Behaviors.Add(new WebHttpBehavior());

            EndpointAddress myEndpointAdd = new EndpointAddress(new Uri(strAdrHTTP),
                EndpointIdentity.CreateDnsIdentity("localhost"));
            restSEP.Address = myEndpointAdd;


            m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),
                MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

            ServiceDebugBehavior debug = new ServiceDebugBehavior();
            debug.IncludeExceptionDetailInFaults = true;

            m_svcHost.Open();
        }

1 个答案:

答案 0 :(得分:1)

这是一个有效的例子 -

Open

哪里 -

[OperationContract]
[WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "login")]
string login(CompositeType request);

样品申请 -

[DataContract]
public class CompositeType
{
    [DataMember]
    public string username { get; set; }
    [DataMember]
    public string password { get; set; }
}

它有效 -

enter image description here

相关问题