日期:2014-05-18  浏览次数:20462 次

C#异步请求Hotmail的问题
参考了网上的资料,自己写了一段异步连接Hotmail的代码,调试中,总发现401错误(未经授权),全部代码如下:

public   class   HotmailProxy
{
private   ManualResetEvent   _allDone;//异步操作是否完成
private   const   int   _timeOut   =   30   *   1000;//30秒超时
private   string   _request;//请求字符串
private   string   _requestMethod;//请求方法
private   Uri   _destination;//目的地
private   NetworkCredential   _credential;//包含用户名/密码
private   CookieContainer   ckContainer;//包含Cookie的容器

//----------------------入口,调用该方法报401错误---------------------------------------------
public   string   Connect(string   username,   string   password)
{
                        //HotMail入口
                        string   hotmailEntryPoint   =   "http://services.msn.com/svcs/hotmail/httpmail.asp ";
                        Uri   des   =   new   Uri(hotmailEntryPoint);

hHttp   =   new   HotmailProxy(username,   password);


//获取根文件夹查询
      string   query   =   " ";
                        query   +=   " <?xml   version= '1.0 '?> ";
query   +=   " <D:propfind   xmlns:D= 'DAV: '   xmlns:h= 'http://schemas.microsoft.com/hotmail/ '   xmlns:hm= 'urn:schemas:httpmail: '> ";
query   +=           " <D:prop> ";
query   +=                   " <hm:msgfolderroot/> ";
query   +=           " </D:prop> ";
query   +=   " </D:propfind> ";

                      string   response   =   hHttp.AsyncSendRequest(query,   "PROPFIND ",   des);
                        //   验证响应
                        if   (response   ==   null   ||   response.Trim().Length   ==   0)
                                throw   new   ApplicationException( "No   response   received   from   host. ");

                        return   response;
}


public   HotmailProxy(string   userName,   string   password)
{
_credential   =   new   NetworkCredential(userName,   password);
_allDone   =   new   ManualResetEvent(false);
ckContainer   =   new   CookieContainer();
}

public   string   AsyncSendRequest(string   request,   string   method,   Uri   destination)
{
return   this.AsyncSendRequest(request,   method,   destination,   _credential);
}

private   string   AsyncSendRequest(string   req