问题:当通过 https 客户端发布到 WCF 休息服务时收到 400 错误请求。
这一切都在没有问题的测试环境中工作。测试 web 获取通过 https 工作,但 post 方法失败并出现 400 错误请求。
test 和 prod 之间的唯一区别是 web 配置,我必须添加行为和绑定才能使测试 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
}
}
全球.asax:
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("uploads", new WebServiceHostFactory(), typeof(Uploader)));
}
网络配置:
<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();
}