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 S开发者_Python百科OAP = "<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
精彩评论