问题:当通过https客户端发送到WCF休息服务时,会收到400个错误请求。
这一切都在测试环境中运行,没有任何问题。测试网络可以通过https运行,但是post方法因400错误请求而失败。
测试和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
}
}
的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();
}
答案 0 :(得分:1)
感谢Aaron Hoffman在这篇文章中的回答:Large WCF web service request failing with (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>