• This repository has been archived on 04/Sep/2019
  • Stars
    star
    158
  • Rank 228,927 (Top 5 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

简单、易用、高效 一个有态度的开源.Net Http请求框架!可以用制作爬虫,api请求等等。

HttpCode.Core

本库不推荐在 .NET Core 下使用

简单、易用、高效 一个有态度的开源.Net Http请求框架!

Latest version

HttpCode.CoreHttpCodehttps://github.com/msdn5/HttpCodeLib ) 的.net standard 2.0 实现,.net core、.net framework都可以使用,更改了异步方法的实现。

使用实例

类库基本功能:

  • 基础功能1:基于HttpWebRequest方式 同步/异步 提交数据 包含(Get/Post)
  • 基础功能2:集合常用处理方法.处理页面结果/HTML数据更快捷

HttpCode类库,让你感受一个简易到极致的HTTP编程. 让编程更简易,代码更简洁... 版权声明:
本类库完全开源,只是基于微软官方提供的HttpWebRequest实现同步与异步的请求 HttpCode Coding by 君临(https://github.com/msdn5) 09-27/2014
HttpCode.Core .net standard 2.0 implement by stulzq(xcmaster)09-01/2017

API文档: http://bbs.msdn5.com/forum.php?mod=forumdisplay&fid=37&filter=typeid&typeid=23

这是HttpCode的文档,大部分和HttpCode.Core是一样的,可以参考,但是有一些地方有差异,主要是异步方法,然后移除了Wininet Api

HttpCode.Core中的对象说明

  • HttpHelpers 请求发起对象 请求过程与执行过程都在这个对象中.
  • HttpItems 请求设置对象 请求时需要配置的参数都在这个对象中
  • HttpResults 请求结果对象

1. HttpHelpers

HttpHelpers http = new HttpHelpers(); //发起请求具体对象
http.AsyncGetHtml(); //异步模式获取请求
http.GetHtml(请求设置参数对象); //获取请求结果,参数返回HttpResults(请求结果对象)
http.GetImg(请求结果对象);//方法会根据请求结果返回一个Image对象

2. HttpItems

item.Accept;//请求标头值  如果没需要不用改动 默认为image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* 

item.Allowautoredirect  //支持跳转页面,查询结果将是跳转后的页面 默认为true .如需获取跳转后的url请使用  HttpResults 中的属性: 

ResponseUrl  //获取响应结果的URL(可获取自动跳转后地址) ,如果获取跳转后地址失败,请使用RedirectUrl属性,并设置HttpItems对象的Allowautoredirect =false; 

RedirectUrl  //获取重定向的URL ;使用本属性时,请先关闭自动跳转属性;设置方法如下:设置HttpItems对象的Allowautoredirect =false; 

item.CerPath//证书绝对路径   如果模拟https时需要证书,则使用此属性附加证书. 详细使用 请参考 httpcode使用案例 如何使用httpcode访问https
//使用方法参考 http://bbs.msdn5.com/thread-555-1-1.html 

item.Connectionlimit //最大连接数  默认1024 无特殊需要不必修改 

item.Container //自动处理Cookie对象. 
//使用方法参考 http://bbs.msdn5.com/thread-552-1-1.html 

item.ContentType //请求返回类型默认 application/x-www-form-urlencoded  无特殊需要不必修改

item.Cookie //请求时的字符串类型Cookie
如果不使用自动维护cookie对象 每次使用cookie属性时请调用
new XJHTTP("上一次请求结果cookie","本次结果的cookie").UpdateCookie方法 更新cookie

item.CookieCollection //请求时的CookieCollection 对象,可以从请求结果(HttpResults)的CookieCollection 对象中得到.每次需要赋值.
 
item.Encoding  //返回数据编码默认为NUll,可以自动识别
 
item.Expect100Continue  //当该属性设置为 true 时,使用 POST 方法的客户端请求应该从服务器收到 100-Continue 响应,以指示客户端应该发送要发送的数据。此机制使客户端能够在服务器根据请求报头打算拒绝请求时,避免在网络上发送大量的数据 默认False

item.Header//请求时的header对象;
获取某一项头
item.Header["请求头名称"].toString(); 例如 item.Header["Location"].toString();  //获取跳转后的地址,如果location不存在则异常

//设置新的头部数据
item.Header.Add("请求头名称","请求头具体值"); // 例如 item.Header.Add("自定义头部数据","http://bbs.msdn5.com");

item.IsAjax //异步标志  默认为false 如果数据包头有 X-Requested-With:XMLHttpRequest 则设置IsAjax为true

item.IsToLower//是否设置为全文小写  默认false  无特殊需要不必修改

item.Method//请求方式 默认为GET方式 POST时需要修改为POST
//使用方法参考     http://bbs.msdn5.com/thread-551-1-1.html

item.Postdata//Post请求时要发送的字符串Post数据
//使用方法参考 http://bbs.msdn5.com/thread-551-1-1.html

item.PostdataByte //Post请求时要发送的Byte类型的Post数据  默认仅设置item.Postdata即可,需要上传包时设置本属性.
//使用方法参考 http://bbs.msdn5.com/thread-518-1-1.html 

item.PostDataType// Post的数据类型  默认为PostDataType.String;上传时需修改为Byte
//使用方法参考 http://bbs.msdn5.com/thread-518-1-1.html 

item.ProxyIp // 代理 服务IP

item.ProxyPwd //代理服务器密码

item.ProxyUserName //代理服务器用户名

item.ReadWriteTimeout//默认写入数据超时间 默认 30000 单位:毫秒  秒与毫秒的关系 1秒 = 1000毫秒

item.Referer //来源地址,上次访问地址  如非必要,不用填写

item.ResultType//设置返回类型String和Byte  默认String,请求图片或文件时选择Byte
//使用方法参考 http://bbs.msdn5.com/thread-553-1-1.html

item.Timeout //默认请求超时时间 默认3000 毫秒

item.URL //起请求的URL地址 必须填写

item.UserAgent //客户端访问信息默认Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0 如非必要,不用修改

3. HttpResults

HttpResults hr = new HttpResults();  //具体对象请调用httphelper.gethtml方法获取的请求后的结果对象
 
hr.Container //自动处理cookie对象 无需自行维护
 
hr.Cookie//请求返回的字符串cookie  下次请求时需要自行维护
 
hr.CookieCollection //请求返回的CookieCollection   下次请求时需要自行维护
 
hr.Header // 请求后的header对象,使用方法同httpitems
 
hr.Html //请求后的结果  
 
hr.RedirectUrl //请求跳转后的地址 获取本属性时,请确认httpitems.Allowautoredirect 为false
 
hr.ResponseUrl//自动跳转后的url地址 获取本属性时,请确认httpitems.Allowautoredirect 为true 
 
 
hr.ResultByte //返回的Byte数组 只有ResultType.Byte时才返回数据,其它情况为空
//使用方法参考 http://bbs.msdn5.com/thread-553-1-1.html]http://bbs.msdn5.com/thread-553-1-1.html
 
hr.StatusCode //Http请求后的状态码
 
hr.StatusDescription //请求后的状态信息          

使用HttpCode.Core发起Get请求

  1. 方法一 使用HttpHelpers类获取结果
string url = "bbs.msdn5.com";//请求地址
 
string res = string.Empty;//请求结果,请求类型不是图片时有效
 
System.Net.CookieContainer cc = new System.Net.CookieContainer();//自动处理Cookie对象
 
HttpHelpers helper = new HttpHelpers();//发起请求对象
 
HttpItems items = new HttpItems();//请求设置对象
 
HttpResults hr = new HttpResults();//请求结果
 
items.URL = url; //设置请求地址
 
items.Container = cc;//自动处理Cookie时,每次提交时对cc赋值即可
 
hr = helper.GetHtml(items);//发起请求
 
res = hr.Html;//得到请求结果
  1. 方法二 使用XJHTTP类获取结果
XJHTTP xjhttp = new XJHTTP();
 
CookieContainer cc = new CookieContainer();//自动处理cookie
 
string referer = "www.msdn5.com"; //referer头数据,如果没有请留空
 
string postdata = "这里是提交的数据"; //post提交数据
 
HttpResults hr = xjhttp.PostHtml(PostUrl, referer, postdata, false, cc);
 
参数说明:
xjhttp.PostHtml("提交的url", "Referer数据头","提交的数据", "Ajax标志不需要时请填写false", "自动处理Cookie对象");

使用HttpCode.Core发起Post请求

  1. 方法一: 使用HttpHelpers类获取结果
string url = "bbs.msdn5.com";//请求地址
 
string res = string.Empty;//请求结果,请求类型不是图片时有效
 
string pdata = "post时所需要的数据";//提交数据(必须项)
 
System.Net.CookieContainer cc = new System.Net.CookieContainer();//自动处理Cookie对象
 
HttpHelpers helper = new HttpHelpers();//发起请求对象
 
HttpItems items = new HttpItems();//请求设置对象
 
HttpResults hr = new HttpResults();//请求结果
 
items.URL = url;//请求的url地址
 
items.Referer = "referer如果有请携带"; //referer头,如果需要请填写
 
items.Container = cc;//自动处理Cookie时,每次提交时对cc赋值即可
 
items.Postdata = pdata;//提交的数据
 
items.Method = "Post";//设置提交方式为post方式提交(默认为Get方式提交)
 
hr = helper.GetHtml(items);//发起请求并获得结果
 
res = hr.Html;//得到请求结果
  1. 方法二: 使用XJHTTP类获取结果
XJHTTP xjhttp = new XJHTTP();
 
CookieContainer cc = new CookieContainer();//自动处理cookie
 
string referer = "www.msdn5.com"; //referer头数据,如果没有请留空
 
string postdata = "这里是提交的数据"; //post提交数据
 
HttpResults hr = xjhttp.PostHtml(PostUrl, referer, postdata, false, cc);
 
参数说明:
xjhttp.PostHtml("提交的url", "Referer数据头","提交的数据", "Ajax标志不需要时请填写false", "自动处理Cookie对象");

如何为httpcode 设置字符串Cookie与自动维护字符串Cookie

自动维护就是指根据每次请求的Response返回的Cookie,来对本地的Cookie进行更新

  1. 手动维护Cookie
HttpItems items = new HttpItems();//请求设置对象

HttpHelpers helper = new HttpHelpers();//发起请求对象

HttpResults hr = new HttpResults();//请求结果对象

string cookie = "";//字符串方式处理

items = new HttpItems(); //初始化请求对象,使其保持最新状态

items.Cookie = cookie;  //使用字符串方式

items.Url = "http://www.163.com/opt.php?do=login"; //请求URL地址

items.Referer = "http://mrtx.163.com/"; //请求的Referer

items.Method = "Post";//请求方式  Post

items.Postdata = "username=ceshiyixia1&password=111111";//提交参数

hr = helper.GetHtml(items); //正常模式得到请求结果

//手动处理字符串Cookie方式   每次都需要剔除返回的hr.Cookie,并且需要与原先的cookie合并为新的Cookie

string CookieStr = new XJHTTP().ClearCookie(hr.Cookie); //首先,剔除 服务器返回cookie中的无用项 

cookie = new XJHTTP().UpdateCookie(CookieStr, hr.Cookie);//接着手动合并两个Cookie ,组成新的cookie给字符串对象使用

//第二次请求
items = new HttpItems();

items.Url = "http://mrtx.u193.com/";

items.Referer = "http://mrtx.u193.com/";

items.Cookie = cookie;

hr = helper.GetHtml(items);
  1. 自动维护字符串Cookie
HttpItems items = new HttpItems();//请求设置对象
 
HttpHelpers helper = new HttpHelpers();//发起请求对象
 
HttpResults hr = new HttpResults();//请求结果对象
 
string cookie = "";//字符串方式处理
 
items = new HttpItems(); //初始化请求对象,使其保持最新状态
 
items.Cookie = cookie;  //使用字符串方式
 
items.URL = "http://www.163.com/opt.php?do=login"; //请求URL地址
 
items.Referer = "http://mrtx.163.com/"; //请求的Referer
 
items.Method = "Post";//请求方式  Post
 
items.Postdata = "username=ceshiyixia1&password=111111";//提交参数
 
#region 自动处理字符串Cookie方式(新)  
 
//使用ref将cookie传递进  GetHtml  重载方法    每次会   自动合并   上次  与  本次返回的cookie
 
hr = helper.GetHtml(items,ref cookie); 
 
#endregion
//多次请求
items = new HttpItems();
 
items.URL = "http://mrtx.u193.com/";
 
items.Referer = "http://mrtx.u193.com/";
 
items.Cookie = cookie;
 
//注意 如果有其他请求 请使用以下调用方式,保持cookie每次被自动合并
hr = helper.GetHtml(items,ref cookie);
  1. 手动处理Cookie的几种常用方式:
new XJHTTP().UpdateCookie("旧Cookie", "请求后的Cookie");//合并Cookie,将cookie2与cookie1合并更新 返回字符串类型Cookie
 
new XJHTTP().StringToCookie("网站Domain", "字符串Cookie内容");//将文字Cookie转换为CookieContainer 对象
 
new XJHTTP().CookieTostring("CookieContainer 对象");//将 CookieContainer 对象转换为字符串类型
 
new XJHTTP().GetAllCookie("CookieContainer 对象");//得到CookieContainer中的所有Cookie对象集合,返回List<Cookie>
 
new XJHTTP().GetCookieByWininet("网站Url");//从Wininet 中获取字符串Cookie 可获取IE与Webbrowser控件中的Cookie
 
new XJHTTP().GetAllCookieByHttpItems("请求设置对象");//从请求设置对象中获取Cookie集合,返回List<Cookie>
 
new XJHTTP().ClearCookie("需要处理的字符串Cookie");//清理string类型Cookie.剔除无用项返回结果为null时遇见错误.
 
new XJHTTP().SetIeCookie("设置Cookie的URL", "需要设置的Cookie");//可设置IE/Webbrowser控件Cookie
 
new XJHTTP().CleanAll();//清除IE/Webbrowser所有内容 (注意,调用本方法需要管理员权限运行) CleanHistory();清空历史记录  CleanCookie(); 清空Cookie CleanTempFiles(); 清空临时文件

更多介绍与示例,请看在线文档:http://bbs.msdn5.com/forum.php?mod=forumdisplay&fid=37&page=1&filter=typeid&typeid=23

More Repositories

1

azure-openai-proxy

Azure OpenAI Service Proxy. Convert OpenAI official API request to Azure OpenAI API request. Support GPT-4,Embeddings,Langchain. Adapter from OpenAI to Azure OpenAI.
Go
1,177
star
2

IdentityServer4.Samples

Samples for IdentityServer4,use .net core 2.0
JavaScript
608
star
3

Alipay.Demo.PCPayment

支付宝PC网站支付、扫码支付 演示项目,实现支付、支付同步回调、支付异步通知、订单查询、退款、退款查询、订单关闭、扫码支付功能。采用支付宝服务端SDK:Alipay.AopSdk.Core,使用ASP.NET Core MVC 。
HTML
282
star
4

RSAUtil

.NET Core RSA algorithm using the help tool.It supports data encryption, decryption, signature and verification signature.It supports three key formats, namely: xml, pkcs1, pkcs8.It also supports key conversion for these three formats.Last also support pem formatting.
C#
273
star
5

dotnet-cnblogs-tool

博客园文章快捷发布工具
C#
166
star
6

IPTools

Querying IP address information, support longitude and latitude. 快速查询IP信息,支持国内和国外IP信息查询,支持查询经纬度。
C#
154
star
7

RSAExtensions

.NET Core >=3.0 & .NET Standard >=2.2, RSA Enhanced Extension. Provide XML format key import and export. Unified export and import of PKCS # 1, PKCS # 8, and XML formats. Supports PKCS # 1, PKCS # 8 PEM import and export.
C#
149
star
8

SolutionRenamer

Fast rename solution,using .net core 2.0
C#
145
star
9

Hangfire.MySql.Core

Hangfire Mysql storage components, support for. NET core 1.1,. NET core 2.0,. NET standard 2.0. Based on Hangfire.MySqlStorage, some bugs were fixed and .NET standard 2.0 support was provided.
C#
83
star
10

ToolGood.Words.Core

一款高性能敏感词(非法词/脏字)检测过滤组件,附带繁体简体互换,支持全角半角互换,汉字转拼音,模糊搜索等功能。支持.net standard 2.0
C#
71
star
11

DotnetCore.RSA

.NET Core use RSA examples,implement RSA/RSA2's Encrypt,Decrypt,Sign,Verify Sign.Use OpenSsl's private/public key.
C#
69
star
12

UrlFirewall

UrlFirewall is a lightweight, fast filtering middleware for http request urls.It supports blacklist, whitelist mode.Supports persisting filter rules to any media.You can use it in webapi, gateway, etc.
C#
66
star
13

BlogDemos

C#
62
star
14

NConsul

.NET API for Consul (http://www.consul.io/)
C#
60
star
15

BlogTools

博文快速多渠道发布工具包,支持博客园、阿里云栖社区、腾讯云+社区、慕课网手记 4种渠道。
C#
36
star
16

AspNetCore.Authentication.ApiToken

A asp.net core webapi token authentication & generator open source library.
C#
32
star
17

DotnetRSA

DotnetRSA is a .NET Core Global Tool.Dotnet RSA Tool can help you generate xml pkcs1, pkcs8 three kinds of format keys, and supports three types of mutual conversion.
C#
27
star
18

dotnetcore-image

Solution of .NET Core GDI+(Image) on Linux/Docker
C#
21
star
19

QRConsole

Print QRCode to console.
C#
16
star
20

ClickHouse.DapperExtensions

Make ClickHouse available to Dapper
C#
14
star
21

NZookeeper

A zookeeper client library based on ZookeeperEx,easily use for Zookeeper.
C#
14
star
22

FreeCert

Fast and easy generation of HTTPS certificate library (Let's Encrypt ACME).
C#
13
star
23

EntityFrameworkCore2Demo

.NET Core控制台、ASP.NET Core程序使用EntityFrameworkCore 2.0示例,包括CodeFirst、DBFirst。详细文章介绍请看:http://www.cnblogs.com/stulzq/p/7717873.html
C#
12
star
24

Influxdb.BulkInsert

Influxdb asynchronous, fast and bulk insert components.Using HTTP or UDP protocol.Influxdb 异步、快速、批量插入组件,支持http和udp协议。
C#
11
star
25

CnBlogSubscribeTool

CnBlogSubscribeTool can crawl blog home page data at regular intervals.
C#
10
star
26

DynamicDns

动态DNS,支持多个云厂商。
C#
9
star
27

ChinaRegionCode

中华人民共和国县以上行政区划代码数据
C#
9
star
28

AspNetCoreHighConcurrency.Sample

AspNetCore High Concurrency Simple Code.Describes how to use distributed locking based on redis. Lock-free and queued commodity inventory operations.
C#
8
star
29

go-linecount

Fast text file line count tool.
Go
8
star
30

kong-plugin-rate-limiting-ex

Rate Limiting Ex is the rate limit plugin for kong gateway.Based on official rate-limiting plugin, I added a property that can be customized to get the request header for the ip address.
Lua
8
star
31

ClientUpdate

Update you c/s software
C#
7
star
32

hexo-deploy-agent

Go
7
star
33

EntityFramework-Core-Migrator

EntityFramework Core Migrator
C#
6
star
34

StudyRoad

6
star
35

MonitoringSystem

prometheus & grafana help doc.
5
star
36

JenkinsSharedLibraries

Jenkins Shared Libraries
Groovy
3
star
37

hexo-statistics

Go
3
star
38

DotNetActionsExample

.NET Github Action Example
C#
3
star
39

Damocles

Open source permission system.Under development now.
C#
3
star
40

MailHelper

C#
2
star
41

blog-demo-go

my blog's article demo
Go
2
star
42

WhoisClient

A simple domain whois lookup library.
C#
2
star
43

Visual-Studio-2013-Theme-Editor

Visual Studio 2013 Theme Editor
C#
1
star
44

TestJenkins

1
star
45

Zeta

A mysterious code repository.
C#
1
star
46

stulzq

1
star
47

WindowsRemoteDesktopCleanTool

Clean up windows remote desktop records
C#
1
star