wcf - 如何处理 Http Error 405 Method Not Allowed?

标签 wcf http cors

我正在尝试从 ajax GET/POST 访问托管在控制台应用程序上的 REST 服务。由于两个应用程序将使用不同的端口,我配置了 REST 服务来处理跨源请求。我已经尝试了所有可用的解决方案,但我仍然无法从 ajax 访问该服务并收到 405。

我正在分享由 http 分析器捕获的原始请求-响应流。

OPTIONS /Service/Contacts?_145***************** HTTP/1.1
Accept: */*
Origin: http:localhost:1053
Access-Control-Request-Method: GET
Access-Control-Request-Headers: access-control-allow-origin, access-control-allow-methods
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: bhawesh-pc:8000
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache
Content-Length: 0
HTTP/1.1 405 Method Not Allowed
Allow: POST, GET
Content-Type: text/html; charset=UTF-8
Server: Microsoft-HTTPAPI/2.0
Access-Control-Allow-Origin: *
Access-Control-Request-Method: POST,GET,PUT,DELETE,OPTIONS
Access-Control-Allow-Headers: X-Requested-With,Content-Type
Date: Tue, 05 Jan 2016 17:57:44 GMT
Content-Length: 1565

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Service</p>
      <p>Method not allowed.</p>
    </div>
  </body>
</html>

REST 服务配置基于http://enable-cors.org/server_wcf.htmlhttp://www.khalidabuhakmeh.com/rest-http-verbs-and-iis-express

最佳答案

这不是有效的 HTTP GET 请求:

Access-Control-Request-Method: GET
Access-Control-Request-Headers: access-control-allow-origin, access-control-allow-methods
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: bhawesh-pc:8000
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache
Content-Length: 0

GET 请求的第一行应该是:

GET /wanted_resource HTTP/1.1

因此,如果服务器查看 header 开头的请求方法,它会看到:

访问ss-Control-...

并将返回一个 405 不允许的方法。

此外,使用GET,不需要Content-Length header 。最好删除该行。

关于wcf - 如何处理 Http Error 405 Method Not Allowed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618927/

相关文章:

asp.net - WCF 和 REST 与 SOAP

.net - 如何强制 WebRequest 在 POST 期间发送授权 header

c# - 套接字在 X 消息后停止接收

javascript - 使用 Angular 获取数据 API 调用时出错

wcf - 如何使用命令行工具将 .net 4 安装到 IIS

c# - WSDL 未在 .NET WCF 服务中加载

ajax - JSON REST Get-Request 正在等待 Tomcat for Chrome/Firefox 上的 CORS

c# - 在 EnableCors() 中允许多个来源

image - 字体和图像跨域资源共享

.net - Spring.net WCF 服务和多线程 : manage count thread for exec WCF method