Error :The remote server returned an error: (401) Unauthorized

后端 未结 3 2111
野的像风
野的像风 2020-12-29 23:42

I want get picture of internet and insert into word .

I use this code .

MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
System.N         


        
相关标签:
3条回答
  • 2020-12-30 00:10

    I add credentials for HttpWebRequest.

    myReq.UseDefaultCredentials = true;
    myReq.PreAuthenticate = true;
    myReq.Credentials = CredentialCache.DefaultCredentials;
    
    0 讨论(0)
  • 2020-12-30 00:20

    The answers did help, but I think a full implementation of this will help a lot of people.

    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");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 00:25

    Shouldn't you be providing the credentials for your site, instead of passing the DefaultCredentials?

    Something like request.Credentials = new NetworkCredential("UserName", "PassWord");

    Also, remove request.UseDefaultCredentials = true; request.PreAuthenticate = true;

    0 讨论(0)
提交回复
热议问题