本文介绍了WebResponse - 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用url(到SharePoint aspx页面)创建WebRequest,并调用GetResponse()方法。我正在通过DefaultCredentials,我甚至已经通过我自己的域用户名和密码,但在GetResponse()方法上,
从服务器抛出401 Unauthorized访问错误。我的应用程序支持经理说我必须将服务帐户添加到站点上的所有者组,这不起作用。没有什么工作,我攻击这个代码的每个角度都会返回401错误。请有人
可以帮助我并指出正确的方向,或者我错过了应该添加的内容。

私有字符串GetHTMLContents(string url) {
string html = string.Empty;
try {
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(url);
request.Method =" GET" ;;
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Regex regex = new Regex(" div class = \" emailContentGroup \">(。*)< / div>",RegexOptions.Singleline);
Stream stream = null;
stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
匹配匹配= regex.Match(reader.ReadToEnd());
html + = match.Groups [0] .Value;
} catch(Exception ex){
Response.Write(ex.GetBaseException());
}
返回html;
}

谢谢!



http://forums.asp.net/



Hi,

I'm trying to create a WebRequest with a url (to a SharePoint aspx page) and invoke the GetResponse() method. I am passing through DefaultCredentials, I have even passed through my own domain user name and password but on the GetResponse() method it throws a 401 Unauthorized access error from the server. My App Support manager said I must add the service account to the Owner group on the site, that did not work. Nothing is working, every angle I attack this code it returns a 401 error. Please can someone help me and point me in the right direction, or did I miss something that should be added.

private string GetHTMLContents(string url) {
            string html = string.Empty;
            try {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.UseDefaultCredentials = true;
                request.PreAuthenticate = true;
                request.Credentials = CredentialCache.DefaultCredentials;
                WebResponse response = request.GetResponse();
                Regex regex = new Regex("<div class=\"emailContentGroup\">(.*)</div>", RegexOptions.Singleline);
                Stream stream = null;
                stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                Match match = regex.Match(reader.ReadToEnd());
                html += match.Groups[0].Value;
            } catch (Exception ex) {
                Response.Write(ex.GetBaseException());
            }
            return html;
        }

Thank you!

解决方案


这篇关于WebResponse - 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 18:04