【问题标题】:Error :The remote server returned an error: (401) Unauthorized错误:远程服务器返回错误:(401)未经授权
【发布时间】:2012-04-29 15:44:23
【问题描述】:

我想获取互联网图片并插入到 word 中。

我使用这个代码。

MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
System.Net.WebRequest request = 
    System.Net.HttpWebRequest.Create("http://spsdev2:1009");

System.Net.WebResponse response = request.GetResponse();
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
//Send an HTTP request and get the image at the URL as an HTTP response
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(fileName);
WebResponse myResp = myReq.GetResponse();

//Get a stream from the webresponse
Stream stream = myResp.GetResponseStream();

myReq.GetResponse() 中出现错误;

错误:远程服务器返回错误:(401) Unauthorized.

编辑

这段代码对我有用:)

myReq.UseDefaultCredentials = true;

myReq.PreAuthenticate = true;

myReq.Credentials = CredentialCache.DefaultCredentials;

【问题讨论】:

  • 另一个帖子上的这个answer 对我有用。

标签: asp.net sharepoint sharepoint-2010


【解决方案1】:

我为HttpWebRequest 添加凭据。

myReq.UseDefaultCredentials = true;
myReq.PreAuthenticate = true;
myReq.Credentials = CredentialCache.DefaultCredentials;

【讨论】:

    【解决方案2】:

    您不应该为您的网站提供凭据,而不是传递 DefaultCredentials 吗?

    类似request.Credentials = new NetworkCredential("UserName", "PassWord");

    另外,删除request.UseDefaultCredentials = true; request.PreAuthenticate = true;

    【讨论】:

    • 您是否更改了可以实际访问您网站的用户名和密码?
    • 你可以删除行 request.UseDefaultCredentials = true; request.PreAuthenticate = true;
    【解决方案3】:

    答案确实有帮助,但我认为全面实施此方法将帮助很多人。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace Dom
    {
        class Dom
        {
            public static string make_Sting_From_Dom(string reportname)
            {
                try
                {
                    WebClient client = new WebClient();
                    client.Credentials = CredentialCache.DefaultCredentials;
                    // Retrieve resource as a stream               
                    Stream data = client.OpenRead(new Uri(reportname.Trim()));
                    // Retrieve the text
                    StreamReader reader = new StreamReader(data);
                    string htmlContent = reader.ReadToEnd();
                    string mtch = "TILDE";
                    bool b = htmlContent.Contains(mtch);
    
                    if (b)
                    {
                        int index = htmlContent.IndexOf(mtch);
                        if (index >= 0)
                            Console.WriteLine("'{0} begins at character position {1}",
                            mtch, index + 1);
                    }
                    // Cleanup
                    data.Close();
                    reader.Close();
                    return htmlContent;
                }
                catch (Exception)
                {
                    throw;
                }
            }
    
            static void Main(string[] args)
            {
                make_Sting_From_Dom("https://www.w3.org/TR/PNG/iso_8859-1.txt");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 2016-01-17
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2021-12-02
      • 2012-09-19
      相关资源
      最近更新 更多