为什么此异常声称代码不写入数据?
如果运行此代码,它将抛出 WebException。内部异常是“无法为不写入数据的操作设置内容长度或分块编码”。我不明白问题的本质。有人能照亮这个黑暗的角落吗?
using System.Diagnostics;
using System.Net;
using System.Text;
namespace sandpit
{
static class Program
{
static void Main()
{
string INITIAL_URI = "http://docs.live.net/SkyDocsService.svc";
string SOAP = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><GetWebAccountInfoRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/clouddocuments\"><BaseRequest><ClientAppId>SkyDrive Service Client</ClientAppId><Market>en-US</Market><SkyDocsServiceVersion>v1.0</SkyDocsServiceVersion></BaseRequest><GetReadWriteLibrariesOnly>false</GetReadWriteLibrariesOnly></GetWebAccountInfoRequest></s:Body></s:Envelope>";
using (WebClient wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
wc.Headers["SOAPAction"] = "GetWebAccountInfo";
wc.Headers["Accept-Language"] = "en-US";
wc.Headers["Accept"] = "text/xml";
wc.Headers["Content-Type"] = "text/xml; charset=utf-8";
string response = wc.UploadString(INITIAL_URI, SOAP);
Debug.WriteLine(response);
}
}
}
}
If you run this code it will throw a WebException. The inner exception is "Content-Length or Chunked Encoding cannot be set for an operation that does not write data." and I do not understand the nature of the problem. Can anyone cast light into this dark corner?
using System.Diagnostics;
using System.Net;
using System.Text;
namespace sandpit
{
static class Program
{
static void Main()
{
string INITIAL_URI = "http://docs.live.net/SkyDocsService.svc";
string SOAP = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><GetWebAccountInfoRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/clouddocuments\"><BaseRequest><ClientAppId>SkyDrive Service Client</ClientAppId><Market>en-US</Market><SkyDocsServiceVersion>v1.0</SkyDocsServiceVersion></BaseRequest><GetReadWriteLibrariesOnly>false</GetReadWriteLibrariesOnly></GetWebAccountInfoRequest></s:Body></s:Envelope>";
using (WebClient wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
wc.Headers["SOAPAction"] = "GetWebAccountInfo";
wc.Headers["Accept-Language"] = "en-US";
wc.Headers["Accept"] = "text/xml";
wc.Headers["Content-Type"] = "text/xml; charset=utf-8";
string response = wc.UploadString(INITIAL_URI, SOAP);
Debug.WriteLine(response);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是网络服务器的重定向。
不幸的是,您必须子类化 WebClient 才能解决此问题。这比看起来更难,因为 Silverlight(任何风格)不喜欢这样,并且会抛出与继承相关的异常,直到您猜测需要重写该构造函数并将其属性为 SecurityCritical。
如果您想更进一步,您可以在 WebClient2 上显示一个 AllowAutoRedirect 属性并将其全部连接起来。
The problem is redirection by the webserver.
Unfortunately you have to subclass WebClient to fix this. This is harder than it looks because Silverlight (any flavour) doesn't like this and throws an inheritance related exception until you guess that you need to override the ctor and attribute it as SecurityCritical.
If you want to go further you could surface an AllowAutoRedirect property on WebClient2 and hook it all up.
使用这个 SkyDrive 客户端而不是手动编写 SOAP 请求怎么样:
对我来说似乎更容易。但是,如果您确实坚持使用 WebClient 并手动处理协议,您可以使用 Fiddler 查看与 .NET 客户端通过网络交换的内容并进行复制。我认为在使用 WebClient 进行身份验证时,您可能缺少为您的请求提供凭据。
如果您查看网络级别跟踪,您会发现 SyDrive 服务器需要 Passport 身份验证,如果您决定使用
WebClient
路由,则必须手动处理该身份验证,这可能需要大量工作:How about using a this SkyDrive client instead of writing SOAP requests manually:
Seems easier to me. But if you really insist on using a
WebClient
and handle the protocol manually you could see with Fiddler what's being exchanged over the wire with the .NET Client and replicate it. I think you might be missing to provide credentials to your request when working with the WebClient in order to authenticate.If you look at the network level trace you will see that the SyDrive server requires Passport authentication which you will have to handle manually if you decide to go with the
WebClient
route which could be quite a lot of work:在我的例子中,问题是由同一 IIS 服务器 6 进行的重定向,因为会话状态模式设置为“自动检测”而不是“使用 cookies”。每个 URL 请求均使用“AspxAutoDetectCookieSupport=1”进行重定向。
In my case problem was redirection made by the same IIS server 6 because Session State Mode was set to 'autodetect' instead of 'use cookies'. Every URL request was redirected with 'AspxAutoDetectCookieSupport=1'.
您只需将 URL 服务更改为: https://docs.live.net/SkyDocsService.svc
You have just to change the URL service into : https://docs.live.net/SkyDocsService.svc