我要获取互联网图片并插入单词中。

我使用此代码。

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 - 错误:远程服务器返回错误:(401)未经授权,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10205854/

10-12 05:38