asp.net – 错误:远程服务器返回错误:(401)未经授权

前端之家收集整理的这篇文章主要介绍了asp.net – 错误:远程服务器返回错误:(401)未经授权前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要获取互联网的图片并插入字词.

我使用这段代码.

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)未经授权.

编辑

这段代码为我工作:)

myReq.UseDefaultCredentials = true;

myReq.PreAuthenticate = true;

myReq.Credentials = CredentialCache.DefaultCredentials;

解决方法

添加了HttpWebRequest的凭据.
myReq.UseDefaultCredentials = true;
myReq.PreAuthenticate = true;
myReq.Credentials = CredentialCache.DefaultCredentials;

猜你在找的asp.Net相关文章