나는 딜러 웹 사이트에서 새로운 자동차 가격을받는 것에 관한 프로젝트를 진행 중입니다. 나는 대부분의 웹 사이트 html을 불러올 수있다. 하지만 그들 중 하나를로드하려고하면 WebGet.Load (url) 메서드는 Object reference not set to an instance of an object.
가 Object reference not set to an instance of an object.
오류. 나는이 웹 사이트들 사이에 어떤 차이점을 발견 할 수 없었다.
정상적으로 작동하는 URL 예 :
http://www.renault.com.tr/page.aspx?id=1715
http://www.hyundai.com.tr/tr/Content.aspx?id=fiyatlistesi
웹 사이트 문제 :
http://www.fiat.com.tr/Pages/tr/otomobiller/grandepunto_fiyat.aspx
도와 줘서 고마워.
var webGet = new HtmlWeb();
var document = webGet.Load("http://www.fiat.com.tr/Pages/tr/otomobiller/grandepunto_fiyat.aspx");
이 URL을 사용하면 문서가로드되지 않습니다.
실제 문제는 HtmlAgilityPack 내부에 있습니다. 작동하지 않는 페이지는 다음 메타 컨텐트 유형을 가지고 있습니다. <META http-equiv="Content-Type" content="text/html; charset=8859-9">
여기서 charset=8859-9
는 참다. HAL 내부는 Encoding.GetEncoding("8859-9")
과 같은 것을 사용하여이 문자열에 적절한 인코딩을 얻으려고 시도하며 오류가 발생합니다 (실제 인코딩은 iso-8859-9
이어야한다고 생각합니다).
사실 당신이 필요로하는 모든이에 대한 인코딩을 읽을하지 HAL을 알리는 것입니다 HtmlDocument
(단지 HtmlDocument.OptionReadEncoding = true
), 그러나 이것은 불가능한 것으로 보인다 HtmlWeb.Load
(설정 HtmlWeb.AutoDetectEncoding
여기서 일하지 않음). 따라서 해결 방법은 URL을 수동으로 읽는 것입니다 (가장 간단한 방법).
var document = new HtmlDocument();
document.OptionReadEncoding = false;
var url =
new Uri("http://www.fiat.com.tr/Pages/tr/otomobiller/grandepunto_fiyat.aspx");
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
document.Load(stream, Encoding.GetEncoding("iso-8859-9"));
}
}
이 작동하고 페이지를 성공적으로 구문 분석합니다.
편집 : @ : Simon Mourier : 네, ArgumentException
catch하고 _declaredencoding = null
설정 때문에 NullReferenceException
합니다. 그리고 _declaredencoding.WindowsCodePage
라인은 null 참조를 던집니다.
다음은 HtmlDocument.cs, ReadDocumentEncoding
메서드의 코드 블록입니다.
try
{
_declaredencoding = Encoding.GetEncoding(charset);
}
catch (ArgumentException)
{
_declaredencoding = null;
}
if (_onlyDetectEncoding)
{
throw new EncodingFoundException(_declaredencoding);
}
if (_streamencoding != null)
{
if (_declaredencoding.WindowsCodePage != _streamencoding.WindowsCodePage)
{
AddError(
HtmlParseErrorCode.CharsetMismatch,
_line, _lineposition,
_index, node.OuterHtml,
"Encoding mismatch between StreamEncoding: " +
_streamencoding.WebName + " and DeclaredEncoding: " +
_declaredencoding.WebName);
}
}
다음은 스택 추적입니다.
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=HtmlAgilityPack
StackTrace:
at HtmlAgilityPack.HtmlDocument.ReadDocumentEncoding(HtmlNode node) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 1916
at HtmlAgilityPack.HtmlDocument.PushNodeEnd(Int32 index, Boolean close) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 1805
at HtmlAgilityPack.HtmlDocument.Parse() in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 1468
at HtmlAgilityPack.HtmlDocument.Load(TextReader reader) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 769
at HtmlAgilityPack.HtmlDocument.Load(Stream stream, Boolean detectEncodingFromByteOrderMarks) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 597
at HtmlAgilityPack.HtmlWeb.Get(Uri uri, String method, String path, HtmlDocument doc, IWebProxy proxy, ICredentials creds) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1515
at HtmlAgilityPack.HtmlWeb.LoadUrl(Uri uri, String method, WebProxy proxy, NetworkCredential creds) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1563
at HtmlAgilityPack.HtmlWeb.Load(String url, String method) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1152
at HtmlAgilityPack.HtmlWeb.Load(String url) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1107
at test.console.Program.Main(String[] args) in W:\Projects\Me\test.console\test.console\Program.cs:line 54
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: