Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want get picture of internet and insert into word .

I use this code .

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(); 

I get error in myReq.GetResponse();

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

Edit

This code work for me :)

myReq.UseDefaultCredentials = true;  myReq.PreAuthenticate = true;  myReq.Credentials = CredentialCache.DefaultCredentials; 
like image 748
ar.gorgin Avatar asked Apr 18 '12 08:04

ar.gorgin


People also ask

What does remote server returned an error mean?

Solution. Overview: The error "The remote server returned an error: (404) Not Found." is displayed when attempting to view an item using OWA (Outlook Web Access) because IIS is not configured properly or the Web services on the Enterprise Vault server is not configured properly.


2 Answers

I add credentials for HttpWebRequest.

myReq.UseDefaultCredentials = true; myReq.PreAuthenticate = true; myReq.Credentials = CredentialCache.DefaultCredentials; 
like image 67
ar.gorgin Avatar answered Sep 19 '22 09:09

ar.gorgin


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;

like image 27
Dante Avatar answered Sep 22 '22 09:09

Dante