简体   繁体   English

WCF Restful服务发布用户名和参数

[英]Wcf Restful service post username with parameter

I'm developer C# .Net I Now programming a Wcf restful service for communication mobile with my server. 我是开发人员C#.Net,我现在正在编程Wcf宁静的服务,以便与服务器进行移动通信。 But a problem in send json request using post method with parameter. 但是使用带参数的post方法发送json请求时出现问题。 After send request from client , server display error: 400 Bad Request ... 客户端 发送请求后,服务器显示错误: 400错误的请求 ...

My service contract : 我的服务合同

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

And implement service: 并实施服务:

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

Edit: 编辑:

My host is: 我的主人是:

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();
        }

This is an working example - 这是一个有效的示例-

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

Where - 哪里-

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

Sample request - 样品要求-

{"request":{"username":"test","password":"test"}}

And it works - 它有效-

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM