繁体   English   中英

无法通过HTTPS 400错误的请求发布到WCF Rest Service

[英]Fail to Post to WCF Rest Service over HTTPS 400 bad request

问题:通过https客户端发布到WCF休息服务时,收到400错误请求。

这一切都可以在没有问题的测试环境中进行。 测试网站的获取可以通过https进行,但是post方法因400错误的请求而失败。

test和prod之间的唯一区别是Web配置,因为我必须添加行为和绑定才能使test webget方法正常工作。 但是我不知道是什么导致帖子失败。

我还尝试过最初添加特定的端点和服务,并找到了使用标准端点的更好解决方案。 因此,我删除了端点和服务,因为应该通过正确的global.asax文件进行注册。

任何帮助,建议或显而易见的事情,我都可以确定现在已经看了100遍了。

服务代码:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Uploader
 {
    [WebGet(UriTemplate = "Test")]
    public String Test()
    {
        return "connected";
    }

    [OperationContract]
    [WebInvoke(UriTemplate = "UploadClaim/{fileName}/{device_id}/{fSize}", Method = "POST")]
    public String UploadClaim(string fileName, string device_id, string fSize, Stream zipFile)
    {
       // Handle post request and store file
    }

}

Global.asax中:

void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.Add(new ServiceRoute("uploads", new WebServiceHostFactory(), typeof(Uploader)));

}

Web.config文件:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceAuthorization principalPermissionMode="None"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding>
      <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" />
        </security>
    </binding>
    <binding name="UnsecureBinding"></binding>
  </webHttpBinding>
</bindings>
<protocolMapping>
  <add scheme="http" binding="webHttpBinding" bindingConfiguration="UnsecureBinding" />
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="UnsecureBinding" />
</protocolMapping>
<standardEndpoints>
  <webHttpEndpoint>
    <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    -->
    <standardEndpoint name="" helpEnabled="true" defaultOutgoingResponseFormat="Json" maxReceivedMessageSize="67108864" 
    transferMode="Buffered">
    <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" />
        </security>
    </standardEndpoint>

  </webHttpEndpoint>
</standardEndpoints>


</system.serviceModel>

客户要求:

  // This url is not the actual but not the problem.  
  //  The only difference from test to  prod is http vs https
requestUrl = "https://MyDomain.com/uploads/UploadClaim/{FILENAME}/{DEVICE_ID}/{FSIZE}";



            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);

            request.Method = "POST";

            request.ContentType = "application/octet-stream";

            request.Timeout = 1000000;

            request.KeepAlive = true;

            request.AllowWriteStreamBuffering = true;

            //request.SendChunked = true;

            //request.ContentType = "application/octet-stream";

            //FileStream inputStream = File.OpenRead(strfPath);

            //request.ContentLength = inputStream.Length;



            byte[] fileToSend = File.ReadAllBytes(strfPath);

            request.ContentLength = fileToSend.Length;



            //Byte[] buffer = new Byte[inputStream.Length];

            ///Stream requestStream = request.GetRequestStream();

            using(Stream requestStream = request.GetRequestStream()){

                bool canWrite = requestStream.CanWrite;

                // Send the file as body request.

                requestStream.Write(fileToSend, 0, fileToSend.Length);



                requestStream.Flush();

                requestStream.Close();

            }

哇,如此明显的队长终于出现了。

感谢本文中Aaron Hoffman的回答: 大型WCF Web服务请求失败,并显示(400)HTTP Bad Request

添加以下诊断信息有助于我确定IIS_USER帐户也没有对我尝试写入的目录的写入权限。 即使该目录位于站点目录中。 向IIS_USER添加了写入文件夹的权限,并且没有其他问题。

希望这可以帮助其他人忽略像我这样的显而易见的人:)

<system.diagnostics>
<sources>
    <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
            <add name="traceListener"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData= "c:\log\Traces.svclog" />
        </listeners>
    </source>
</sources>

暂无
暂无

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

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