这是代码
using (HttpClient client = new HttpClient())
{
// This url is just for testing and the file really exist
byte[] data = client.GetByteArrayAsync("http://pngimg.com/uploads/simpsons/simpsons_PNG96.png").Result; // Returning error 404 here only on IIS
using (MemoryStream mem = new MemoryStream(data))
{
using (var myImg = Image.FromStream(mem))
{
// If you want it as Png
myImg .Save("path_to_your_file.png",ImageFormat.Png);
}
}
}
这在IIS Express中有效,但在IIS服务器上返回错误404。我试图从VS进行调试,但使用IIS(Windows 10Pro),它无法正常工作,在IIS(Windows Server 2016)上部署时也无法正常工作。
该应用程序是基于.NET Core 2.1构建的Web服务。
我是否在IIS或应用程序设置中缺少某些内容才能使其正常工作?
我也尝试以下代码:
using (WebClient client1 = new WebClient())
{
var a = client1.DownloadData(new Uri("http://pngimg.com/uploads/simpsons/simpsons_PNG96.png"));
}
但是我得到相同的结果。
在这两个代码中,我使用IIS Express和VS都获得了200 OK,并且使用Visual Studio调试中的IIS获得了以下结果:
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).CharacterSet "iso-8859-1" string
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).ContentEncoding null string
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).ContentLength 309 long
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).ContentType "text/html; charset=iso-8859-1" string
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).Cookies {System.Net.CookieCollection} System.Net.CookieCollection
((System.Net.WebException)e).Response {System.Net.Httpwebresponse} System.Net.webresponse {System.Net.Httpwebresponse}
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).Headers {Date: Fri,15 Nov 2019 17:46:35 GMT
Server: Apache/2.2.15,(CentOS)
Connection: close
Content-Length: 309
Content-Type: text/html; charset=iso-8859-1
} System.Net.WebHeaderCollection
((System.Net.WebException)e).Response.IsFromCache false bool
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).IsMutuallyAuthenticated false bool
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).LastModified {2019-11-15 1:09:53 PM} System.DateTime
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).Method "GET" string
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).ProtocolVersion {1.1} System.Version
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).ResponseUri {http://pngimg.com/uploads/simpsons/simpsons_PNG96.png} System.Uri
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).Server "Apache/2.2.15,(CentOS)" string
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).StatusCode NotFound System.Net.HttpStatusCode
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).StatusDescription "Not Found" string
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response).SupportsHeaders true bool
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response)._characterSet "iso-8859-1" string
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response)._cookies {System.Net.CookieCollection} System.Net.CookieCollection
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response)._httpResponseMessage {StatusCode: 404,ReasonPhrase: 'Not Found',Version: 1.1,Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent,Headers:
{
Date: Fri,15 Nov 2019 17:46:35 GMT
Server: Apache/2.2.15
Server: (CentOS)
Connection: close
Content-Length: 309
Content-Type: text/html; charset=iso-8859-1
}} System.Net.Http.HttpResponseMessage
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response)._isVersionHttp11 true bool
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response)._requesturi {http://pngimg.com/uploads/simpsons/simpsons_PNG96.png} System.Uri
((System.Net.Httpwebresponse)((System.Net.WebException)e).Response)._webHeaderCollection {Date: Fri,(CentOS)
Connection: close
Content-Length: 309
Content-Type: text/html; charset=iso-8859-1
} System.Net.WebHeaderCollection
错误表单http请求:
(((System.aggregateexception)e)。消息“发生一个或多个错误。(响应状态码不表示成功:404(未找到)。)”字符串