English 中文(简体)
Why does this exception claim the code doesn t write data?
原标题:

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);
      }
    }
  }
}
最佳回答

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.

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

If you want to go further you could surface an AllowAutoRedirect property on WebClient2 and hook it all up.

问题回答

How about using a this SkyDrive client instead of writing SOAP requests manually:

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

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:

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

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 .

You have just to change the URL service into : https://docs.live.net/SkyDocsService.svc





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签