标签: wcf-rest

无法在jQuery.ajax中将content-type设置为'application/json'

当我有这个代码

$.ajax({
    type: 'POST',
    //contentType: "application/json",
    url: 'http://localhost:16329/Hello',
    data: { name: 'norm' },
    dataType: 'json'
});
Run Code Online (Sandbox Code Playgroud)

在Fiddler我可以看到以下原始请求

POST http://localhost:16329/Hello HTTP/1.1
Host: localhost:16329
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:14693/WebSite1/index.html
Content-Length: 9
Origin: http://localhost:14693
Pragma: no-cache
Cache-Control: no-cache

name=norm
Run Code Online (Sandbox Code Playgroud)

但我正在尝试的是将内容类型从application/x-www-form-urlencoded设置application/json.但是这段代码

$.ajax({
    type: "POST",
    contentType: "application/json",
    url: 'http://localhost:16329/Hello',
    data: { name: 'norm' },
    dataType: "json"
});
Run Code Online (Sandbox Code Playgroud)

生成奇怪的请求(我可以在Fiddler中看到)

OPTIONS http://localhost:16329/Hello …
Run Code Online (Sandbox Code Playgroud)

jquery json content-type wcf-rest

96
推荐指数
5
解决办法
35万
查看次数

我们可以创建自定义HTTP状态代码吗?

我有一个REST和WCF服务,并希望根据操作发送自定义状态代码.

某些验证失败然后我想发送HTTP 444的示例,当授权失败时,我想发送HTTP 455

问题是我们如何对SOAP和REST Web服务进行验证.

在客户端上,错误代码如何起作用,因为当您从WCF服务(使用SOAP)发送HTTP 400/500时,会在显示状态代码的客户端上抛出异常.

现在,如果我发送一个新的自定义状态代码,客户端如何处理这个?

c# asp.net wcf http wcf-rest

77
推荐指数
6
解决办法
7万
查看次数

jQuery .ajax()POST请求在RESTful WCF上抛出405(方法不允许)

我正在向RESTFUL WCF服务应用程序发送一个post请求.我能够POST通过Fiddler 成功发送请求.

但是,当我通过jQuery Ajax方法执行此操作时,该函数会将以下内容返回到Chrome Developer Console:

OPTIONS http://www.example.com/testservice/service1.svc/GetData 405 (Method Not Allowed) jquery.min.js:6
Run Code Online (Sandbox Code Playgroud)

但是之后的第二个日志:

Object {d: "You entered 10"} testpost.html:16
Run Code Online (Sandbox Code Playgroud)

这告诉我的是jQuery正在发送一个OPTIONS请求,该请求失败,然后发送一个POST返回预期数据的请求.

我的jQuery代码:

$.ajax() {        
type: "POST", //GET or POST or PUT or DELETE verb 
    url: "http://www.example.com/testservice/service1.svc/GetData", // Location of the service      
    data: '{"value":"10"}', //Data sent to server
    contentType:"application/json",
    dataType: "json", //Expected data format from server    
    processdata: false,
    success: function (msg) {//On Successfull service call   
        console.log(msg);
    },
    error: function (xhr) { console.log(xhr.responseText); } // …
Run Code Online (Sandbox Code Playgroud)

rest jquery post web-services wcf-rest

34
推荐指数
2
解决办法
14万
查看次数

RESTful服务:WCF与ASP.NET MVC

实现RESTful服务的一种非常常见的方法是使用ASP.NET MVC在WCF上执行此操作.

ASP.NET MVC通过灵活的URL路由和灵活的HTTP方法映射到控制器操作,具有出色的RESTful支持.

WCF 4.0现在对使用与ASP.NET MVC相同的ASP.NET路由机制实现RESTful服务提供了出色的支持.

问题 使用两种创建RESTful服务的方法和遇到的优缺点,您有什么经验?

rest asp.net-mvc wcf wcf-rest

20
推荐指数
2
解决办法
6539
查看次数

WCF REST服务中的CORS支持

我有一个在Windows服务中托管的WCF REST服务,我希望每次响应都发送Access-Control-Allow-Origin HTTP标头(定义为CORS的一部分).

我尝试的解决方案是在IDispatchMessageInspector实现中使用以下内容:

public void BeforeSendReply(ref Message reply, object correlationState)
{
    var httpResponse = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
    if (httpResponse != null)
    {
        // test of CORS
        httpResponse.Headers["Access-Control-Allow-Origin"] = "*";
    }
}
Run Code Online (Sandbox Code Playgroud)

通常这可以工作,但不幸的是我的服务也使用HTTP基本授权,这意味着当请求进入而没有Authorization标头时,WCF会自动发送401响应,要求提供凭据.遗憾的是,在此初始交换期间,WCF不会调用我的IDispatchMessageInspector,因此Access-Control-Allow-Origin标头不会添加到初始交换中.

当我尝试从浏览器调用服务时,会出现此问题.CORS指定仅当源域与Access-Control-Allow-Origin响应头中列出的域匹配时才允许跨源请求(*匹配所有域).不幸的是,当浏览器看到没有Access-Control-Allow-Origin标头的初始401响应时,它会阻止访问(根据相同的源策略).

有没有办法在WCF自动发送的初始401响应中添加标头?

.net c# wcf wcf-rest cors

19
推荐指数
2
解决办法
2万
查看次数

WCF REST服务在WCFTestClient中不可见

我已经为我的原型服务成功配置了3个端点.端点是basicHttpBinding,wsHttpBinding和webHttpBinding.我目前唯一的故障是WCFTestClient.当我将它指向我的服务时,它会列出前两个,但不会列出webHttpBinding.我可以通过浏览器测试REST端点,它工作得很好.这是我的配置:

 <system.serviceModel>
    <services>
      <service behaviorConfiguration="serviceBehaviour" name="VMDServices.VMDService">
        <endpoint binding="webHttpBinding"
                  address="rest" behaviorConfiguration="webBehaviour" contract="VMDServices.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint binding="basicHttpBinding"
                  address="basic" bindingConfiguration="basicBinding" contract="VMDServices.IService1">
        </endpoint>
        <endpoint binding="wsHttpBinding"
                  address="ws" bindingConfiguration="wsBinding" contract="VMDServices.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>

    <bindings>
      <basicHttpBinding>
        <binding name="basicBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="None"></security>
          <readerQuotas maxStringContentLength="2147483647"/>
        </binding>
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="wsBinding" transactionFlow="true">
          <security mode="None"></security>
          <reliableSession enabled="true" ordered="true" />
        </binding>
      </wsHttpBinding>
    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below …
Run Code Online (Sandbox Code Playgroud)

wcf wcftestclient webhttpbinding wcf-rest

16
推荐指数
1
解决办法
2万
查看次数

ServiceHost仅支持类服务类型

我有一个名为WcfService2(原件我知道)的服务,它有一个带有公共接口的IService.cs文件:

namespace WcfService2
{
    [ServiceContract]
    public interface IService1
    {    
        [OperationContract]
        [WebGet(UriTemplate = "/{value}")]
        string GetData(string value);            
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我有我的公共类Service1.svc.cs文件,它返回一个值的字符串,如下所示:

namespace WcfService2
{
    public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在正尝试使用如下控制台应用程序来托管此服务:

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            WebHttpBinding binding = new WebHttpBinding();
            WebServiceHost host =
            new WebServiceHost(typeof(IService1));
            host.AddServiceEndpoint(typeof(IService1),
            binding,
            "http://localhost:8000/Hello");
            host.Open();
            Console.WriteLine("I DONT LIKE REST!");
            Console.WriteLine("Press <RETURN> to KILL REST FOR GOOD");
            Console.ReadLine();
        } …
Run Code Online (Sandbox Code Playgroud)

.net c# wcf exception-handling wcf-rest

14
推荐指数
1
解决办法
3万
查看次数

WCF错误:相对端点地址

对于WCF来说,这是一个新手.我想我搞砸了一下.所以这就是我到目前为止所做的,我在IIS中托管了我的WCF服务

首先是合同

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using YangkeeServer.dto;

namespace YangkeeServer
   {

public class Service1 : IService1
{
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "single")]
    public YangkeeTrailerDTO getTrailor()
    {
        return new YangkeeTrailerDTO()
        {
            loanFrom = "heaven"
        };
    }


    [WebInvoke(Method = "GET",
       ResponseFormat = WebMessageFormat.Json,
       UriTemplate = "datum/{id}")]
    public Test getName(string id)
    {
        return new Test()
        {
            Id = Convert.ToInt32(id) * 12,
            Name = "Leo Messi"
        };
    }
}
} …
Run Code Online (Sandbox Code Playgroud)

iis wcf iis-7 wcf-rest

13
推荐指数
2
解决办法
4万
查看次数

为WCF(Rest Service)Framework 4.0实现DotNetOpenAuth(oAuth2)

我已经创建了一个WCF API(Rest Services),我想实现oAuth 2的身份验证协议.

我知道这个库DotNetOpenAuth对于实现WebApi的oAuth2非常有用,但我还没有看到任何解释如何为WCF项目实现DotNetOpenAuth(oAuth2)的文档或示例(Rest Service)(Framework 4.0)

.net-4.0 wcf-rest oauth-2.0

12
推荐指数
1
解决办法
6083
查看次数

WebOperationContext.current和HttpContext.Current之间的区别

我为客户开发Web和移动应用程序.在我目前的架构中,Web访问和移动访问之间共享许多资源.一个aspx页面可以示出通过网站和被调用以在移动应用的web图.我的问题是:

WebOperationContext.CurrentHttpContext.Current对象有什么区别?

根据我的理解,它是同一个对象,但我注意到WebOperationContext.Current在某些情况下为空,我不明白为什么.

c# asp.net wcf-rest

12
推荐指数
1
解决办法
7391
查看次数