如何通过HttpWebRequest访问外部实时会议URL

时间:2012-08-01 14:50:05

标签: c# .net httpwebrequest

我正在尝试使用Httpwebrequest访问外部实时会议网址,并获得401未经授权的错误。相同的代码在我的本地系统中工作。

代码:

     HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(PostingUrl);
     CredentialCache CredMCCache = new CredentialCache();
        myReq.PreAuthenticate = true;
        CredMCCache.Add(new System.Uri(PostingUrl),"Basic",new    System.Net.NetworkCredential("username","password")
 myReq.Credentials = CredMCCache;
        myReq.KeepAlive = true;
        myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
        myReq.Accept = "*/*";
        myReq.Headers.Add("Accept-Language", "en-us");
        myReq.Headers.Add("Accept-Encoding", "gzip, deflate");
        WebProxy proxyObject = new WebProxy("proxy url with port", false);
        myReq.Proxy = proxyObject;
        myReq.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
        myReq.Method = "GET";
        HttpWebResponse myResp = null;


            // Get the response from the conference center
            myResp = (HttpWebResponse)myReq.GetResponse();

我收到上述行中的错误。任何指针都会有所帮助。

1 个答案:

答案 0 :(得分:0)

为什么要设置代理,例如

  

myReq.Proxy = proxyObject;

你需要这样做吗?如果您确实要通过公司代理,则不需要为HttpWebRequest设置代理,因为它将从IE中获取设置(如果有)。

其次,您是否尝试使用基本身份验证来对远程服务器进行身份验证?它看起来像你,所以使用它来代替在标题

中设置authenitcation细节
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
myReq.Headers["Authorization"] = "Basic " + authInfo;