webtoolkit类:
using system;
using system.net.sockets;
using system.text;
namespace consoleapplication1
{
class webtoolkit
{
/// <summary>
/// url结构
/// </summary>
struct urlinfo
{
public string host;
public int port;
public string file;
public string body;
}
/// <summary>
/// 解析url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static urlinfo parseurl(string url)
{
urlinfo urlinfo = new urlinfo();
string[] strtemp = null;
urlinfo.host = "";
urlinfo.port = 80;
urlinfo.file = "/";
urlinfo.body = "";
int intindex = url.tolower().indexof("http://");
if (intindex != -1)
{
url = url.substring(7);
intindex = url.indexof("/");
if (intindex == -1)
{
urlinfo.host = url;
}
else
{
urlinfo.host = url.substring(0, intindex);
url = url.substring(intindex);
intindex = urlinfo.host.indexof(":");
if (intindex != -1)
{
strtemp = urlinfo.host.split(':');
urlinfo.host = strtemp[0];
int.tryparse(strtemp[1], out urlinfo.port);
}
intindex = url.indexof("?");
if (intindex == -1)
{
urlinfo.file = url;
}
else
{
strtemp = url.split('?');
urlinfo.file = strtemp[0];
urlinfo.body = strtemp[1];
}
}
}
return urlinfo;
}
/// <summary>
/// 发出请求并获取响应
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="body"></param>
/// <param name="encode"></param>
/// <returns></returns>
private static string getresponse(string host, int port, string body, encoding encode)
{
string strresult = string.empty;
byte[] btesend = encoding.ascii.getbytes(body);
byte[] btereceive = new byte[1024];
int intlen = 0;
using (socket socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp))
{
try
{
socket.connect(host, port);
if (socket.connected)
{
socket.send(btesend, btesend.length, 0);
while ((intlen = socket.receive(btereceive, btereceive.length, 0)) > 0)
{
strresult += encode.getstring(btereceive, 0, intlen);
}
}
socket.close();
}
catch { }
}
return strresult;
}
/// <summary>
/// get请求
/// </summary>
/// <param name="url"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string get(string url, encoding encode)
{
urlinfo urlinfo = parseurl(url);
string strrequest = string.format("get {0}?{1} http/1.1\r\nhost:{2}:{3}\r\nconnection:close\r\n\r\n", urlinfo.file, urlinfo.body, urlinfo.host, urlinfo.port.tostring());
return getresponse(urlinfo.host, urlinfo.port, strrequest, encode);
}
/// <summary>
/// post请求
/// </summary>
/// <param name="url"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string post(string url, encoding encode)
{
urlinfo urlinfo = parseurl(url);
string strrequest = string.format("post {0} http/1.1\r\nhost:{1}:{2}\r\ncontent-length:{3}\r\ncontent-type:application/x-www-form-urlencoded\r\nconnection:close\r\n\r\n{4}", urlinfo.file, urlinfo.host, urlinfo.port.tostring(), urlinfo.body.length, urlinfo.body);
return getresponse(urlinfo.host, urlinfo.port, strrequest, encode);
}
}
}
调用示例:
using system;
using system.text;
namespace consoleapplication1
{
//调用示例
class program
{
public static void main(string[] args)
{
console.writeline(webtoolkit.get("//www.jb51.net/t.asp?keyword=vbscript", encoding.default));
console.readkey();
}
}
}
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
C#实现猜数字游戏
本文实例为大家分享了c#实现猜数字游戏具体代码,供大家参考,具体内容如下给定一个0-100的随机数字猜其大小题目样式:电脑产生一个0到100之间的随机数字,并且...
[阅读全文]
-
-
C# 可空类型的具体使用
在项目中我们经常会遇到可为空类型,那么到底什么是可为空类型呢?下面我们将从4个方面为大家剖析。1、可空类型基础知识顾名思义,可空类型指的就是某个对象类型可以为空...
[阅读全文]
-
-
-
C#实现猜数字小游戏
本文实例为大家分享了c#实现猜数字小游戏的具体代码,供大家参考,具体内容如下效果如图:代码:using system;using system.collecti...
[阅读全文]
-
-
C#实现简单俄罗斯方块
最近在看《.net游戏编程入门经典 c#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪念。1.类的设计在充...
[阅读全文]
-
网友评论