3

如果您运行此代码,它将引发 WebException。内部异常是“不能为不写入数据的操作设置 Content-Length 或 Chunked Encoding”。我不明白问题的本质。谁能给这个黑暗的角落投光?

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);
      }
    }
  }
}
4

4 回答 4

5

问题是网络服务器重定向。

不幸的是,您必须继承 WebClient 才能解决此问题。这比看起来更难,因为 Silverlight(任何风格)不喜欢这样并抛出与继承相关的异常,直到您猜测您需要覆盖 ctor 并将其属性为 SecurityCritical。

public class WebClient2 : WebClient
{
  [SecurityCritical]
  public WebClient2() : base() { }  
  protected override WebRequest GetWebRequest(System.Uri address)
  {
    var wr = base.GetWebRequest(address);
    if (wr is HttpWebRequest)
      (wr as HttpWebRequest).AllowAutoRedirect = false;
    return wr;
  }
}

如果您想更进一步,您可以在 WebClient2 上显示 AllowAutoRedirect 属性并将其全部连接起来。

于 2011-08-01T01:42:11.783 回答
2

如何使用这个SkyDrive 客户端而不是手动编写 SOAP 请求:

var client = new SkyDriveServiceClient();
client.LogOn("user", "pwd");
var info = client.GetWebAccountInfo();
Console.WriteLine(info.Title);

对我来说似乎更容易。但是,如果您真的坚持使用 aWebClient并手动处理协议,您可以使用 Fiddler 查看与 .NET 客户端通过网络交换的内容并复制它。我认为您在使用 WebClient 进行身份验证时可能缺少为您的请求提供凭据。

如果您查看网络级别跟踪,您将看到 SyDrive 服务器需要 Passport 身份验证,如果您决定使用WebClient可能需要大量工作的路由,则必须手动处理:

Location: https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1310230173&rver=6.1.6206.0&wp=MBI&wreply=http:%2F%2Fdocs.live.net:101%2FSkyDocsService.svc&lc=1033&id=250206
WWW-Authenticate: Passport1.4 ct=1310230292,rver=6.1.6206.0,wp=MBI,lc=1033,id=250206
于 2011-07-09T16:37:54.523 回答
1

在我的情况下,问题是由同一个 IIS 服务器 6 进行的重定向,因为会话状态模式设置为“自动检测”而不是“使用 cookie”。每个 URL 请求都使用“AspxAutoDetectCookieSupport=1”重定向。

于 2015-12-24T11:18:53.403 回答
-1

您只需将 URL 服务更改为:https ://docs.live.net/SkyDocsService.svc

于 2012-06-03T14:01:57.560 回答