3

我正在使用 WebClient.OpenWrite POST 到 .NET 4 中的 WCF HTTP Web 服务。如果我在 WebClient 标头上设置内容类型,那么当流关闭时,我会收到一个 WebException(400 - 错误请求)。

var client = new WebClient();

// Setting ContentType generates a WebException on 400 - Bad Request when the stream is closed
//client.Headers[ HttpRequestHeader.ContentType ] = "application/json";

var stream = client.OpenWrite( "http://localhost:21159/Colors.svc/Color", "POST" );

var serializer = new DataContractJsonSerializer( typeof( ClientColor ) );
serializer.WriteObject( stream, color );
stream.Close();

服务接口如下所示:

[ServiceContract]
public interface IColors
{
  [WebInvoke( UriTemplate = "Color", Method = "POST" )]
  void ColorPost( Stream color );
}

如果我将 Content-Type 标头设置为“application/xxx”,则 POST 可以正常工作。

有什么想法为什么我在 Content-Type 为“application/json”时得到 WebException?

4

0 回答 0