Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ UpgradeLog*.XML

.sass-cache
.sass-cache/*
/.vs/NSoup/v15/Server/sqlite3
/.vs
18 changes: 18 additions & 0 deletions NSoup.Tests/NSoup.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NSoup\NSoup.csproj" />
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions NSoup.Tests/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using NSoup.Nodes;
using NSoup.Parse;
using NSoup.Select;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace NSoup.Tests
{
public class Http
{
static Http()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
private readonly HttpClient httpClient;
private HttpClientHandler httpClientHandler;
public Http(Uri proxyAddrss = null, string proxyUserName = null, string proxyPassword = null)
{


if (proxyAddrss != null)
{
//启用代理
httpClientHandler = new HttpClientHandler()
{
UseProxy = true,
UseCookies = true,
UseDefaultCredentials = false,
//Proxy = new WebProxy(proxyAddrss, true)
//{
// Credentials = !string.IsNullOrWhiteSpace(proxyUserName) ? new NetworkCredential(proxyUserName, proxyPassword) : null
//}
};

}
else
{
httpClientHandler = new HttpClientHandler() { UseCookies = true };
}
httpClient = new HttpClient(httpClientHandler);
httpClient.Timeout = new TimeSpan(1, 0, 0);
//client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");

}

public async Task<string> GetHtml(string url, Dictionary<string, string> headers = null)
{
httpClient.DefaultRequestHeaders.Host = (new Uri(url).Host);
if (headers != null)
{
foreach (var key in headers.Keys)
{
httpClient.DefaultRequestHeaders.Add(key, headers[key]);
}
}

using (var result = await httpClient.GetAsync(url))
{
return await result.Content.ReadAsStringAsync();
}
}
}
public class Test
{
[Fact]
public void TestRun()
{
Http http = new Http();
var html = http.GetHtml("https://dealer.autohome.com.cn/1120/info.html").Result;
Document doc = Parser.Parse(html, "http://dealer.autohome.com.cn/");
Elements elems = doc.Select(".dealeron-cont .show-ul li img");
}
}
}
35 changes: 15 additions & 20 deletions NSoup.sln
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C66E2854-5CFF-4DD3-B867-3FE49DE1E96A}"
ProjectSection(SolutionItems) = preProject
LocalTestRun.testrunconfig = LocalTestRun.testrunconfig
NSoup.vsmdi = NSoup.vsmdi
EndProjectSection
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSoup", "NSoup\NSoup.csproj", "{EA189DC2-2C8D-4B50-BEE8-8964D6BEDF33}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSoup", "NSoup\NSoup.csproj", "{EA189DC2-2C8D-4B50-BEE8-8964D6BEDF33}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{B9BBCF9A-4E79-4A90-8401-C11D4D7A86B0}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSoup.Tests", "NSoup.Tests\NSoup.Tests.csproj", "{3DFFF8C7-74AC-45EB-B391-96AC7B01ADEA}"
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = NSoup.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Expand All @@ -24,16 +17,18 @@ Global
{EA189DC2-2C8D-4B50-BEE8-8964D6BEDF33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA189DC2-2C8D-4B50-BEE8-8964D6BEDF33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA189DC2-2C8D-4B50-BEE8-8964D6BEDF33}.Release|Any CPU.Build.0 = Release|Any CPU
{B9BBCF9A-4E79-4A90-8401-C11D4D7A86B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9BBCF9A-4E79-4A90-8401-C11D4D7A86B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9BBCF9A-4E79-4A90-8401-C11D4D7A86B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9BBCF9A-4E79-4A90-8401-C11D4D7A86B0}.Release|Any CPU.Build.0 = Release|Any CPU
{51AD4E15-891C-45D7-9AA1-B83A69E53B07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{51AD4E15-891C-45D7-9AA1-B83A69E53B07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{51AD4E15-891C-45D7-9AA1-B83A69E53B07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{51AD4E15-891C-45D7-9AA1-B83A69E53B07}.Release|Any CPU.Build.0 = Release|Any CPU
{3DFFF8C7-74AC-45EB-B391-96AC7B01ADEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DFFF8C7-74AC-45EB-B391-96AC7B01ADEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DFFF8C7-74AC-45EB-B391-96AC7B01ADEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DFFF8C7-74AC-45EB-B391-96AC7B01ADEA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DBC92AF-821B-4500-8940-611594BF2095}
EndGlobalSection
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = NSoup.vsmdi
EndGlobalSection
EndGlobal
Loading