http协议区分头信息和正文
http协议中的头信息和正文是採用空行分开,什么是空行呢?简单来说,就是\r\n\r\n。
所以将server返回的数据用\r\n\r\n分开后的结果,一个是头信息。一个是正文信息。
C#的代码例如以下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using System.Text.RegularExpressions; using System.IO; namespace SOCKET
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
int port = 80;
GetSocket gs = new GetSocket();
string result = gs.SocketSendReceive("www.baidu.com", port);
Regex regex = new Regex("\r\n\r\n");//以cjlovefl切割
string[] bit = regex.Split(result);
MessageBox.Show(bit[1]); StreamWriter sw = new StreamWriter("D:\\1.txt");
sw.Write(result);
sw.Close();
}
}
}
当中result就是由server返回的包含头部和正文的信息。
当中GetSocket类例如以下:
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Text.RegularExpressions; public class GetSocket
{
public Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
// This method requests the home page content for the specified server.
public string SocketSendReceive(string server, int port)
{
string request = "GET / HTTP/1.0\r\nHost: " + server +
"\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0\r\nConnection: keep-alive\r\n\r\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(server, port);
if (s == null)
return ("Connection failed");
// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);
// Receive the server home page content.
int bytes = 0;
string page = "Default HTML page on " + server + ":\r\n";
// The following will block until te page is transmitted.
do
{ bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page =page+ Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0); return page;
}
/*
public static void Main(string[] args)
{
string host = "http://www.baidu.com";
int port = 80;
if (args.Length == 0)
// If no server name is passed as argument to this program,
// use the current host name as the default.
host = Dns.GetHostName();
else
host = args[0];
string result = SocketSendReceive("www.goodjobs.cn", port);
StreamWriter sw = new StreamWriter("D:\\1.txt");
string w = "";
sw.Write(result);
sw.Close();
}
* */
}
測试成功!
http协议区分头信息和正文的更多相关文章
- ubuntu系统中出现mysql数据库无法启动报错2002该怎么处理,具体报错信息如正文所示
python@ubuntu:~$ mysql -uroot -pmysqlmysql: [Warning] Using a password on the command line interface ...
- 我为开源做贡献,网页正文提取——Html2Article
为什么要做正文提取 一般做舆情分析,都会涉及到网页正文内容提取.对于分析而言,有价值的信息是正文部分,大多数情况下,为了便于分析,需要将网页中和正文不相干的部分给剔除.可以说正文提取的好坏,直接影响了 ...
- 1.10 基础知识——GP3.1 制度化 & GP3.2 收集改进信息
摘要: GP3.1是要求建立组织级的关于该过程的制度.标准.模版等全套体系,要求覆盖该PA所有的SP和GP.GP3.2 体现的是持续改进,每个过程都应该收集相应的改进信息. 正文: GP3.1 Est ...
- 简易商品信息管理系统——首个Web项目
正文之前 在学习了一段时间的Java Web的内容之后,当然需要有个项目来练练手,我相信大多数人的首选项目都是信息管理系统吧,所以我选择了商品信息管理系统 目前项目源码已全部上传至GitHub,欢迎大 ...
- 【Java Web】简易商品信息管理系统——首个Web项目
正文之前 在学习了一段时间的Java Web的内容之后,当然需要有个项目来练练手,我相信大多数人的首选项目都是信息管理系统吧,所以我选择了商品信息管理系统 目前项目源码已全部上传至GitHub,欢迎大 ...
- apache的AB测试
A/B测试A/B测试是一种新兴的网页优化方法,可以用于增加转化率注册率等网页指标..A/B测试的目的在于通过科学的实验设计.采样样本代表性.流量分割与小流量测试等方式来获得具有代表性的实验结论,并确信 ...
- SQL Server 深入解析索引存储(中)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/堆 概述 本篇文章是关于堆的存储结构.堆是不含聚集索引的表(所以只有非聚集索引的表也是堆).堆的 sys.parti ...
- apache ab测试命令详解
这篇文章主要介绍了apache性能测试工具ab使用详解,需要的朋友可以参考下 网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环.只有让服务器处在高压情况下,才能真正体现出软件.硬件等各种 ...
- 【Java EE 学习 22 上】【文件上传】【目录打散】【文件重命名】
1.文件上传概述 (1)使用<input type="file">的方式来声明一个文件域. (2)表单提交方式一定要是post方式才行 (3)表单属性enctype 默 ...
随机推荐
- margin 百分比是按參照物来计算滴 不知道吧?
<style> #demo{ margin: 0 auto; width: 1000px; height: 500px; background: #eee; overflow: hidde ...
- linux for LVM 创建笔记
LVM: 1.创建pv(物理卷) [root@localhost dev]# pvcreate /dev/sdd /dev/sde /dev/sdf Writing physical volume d ...
- Qt5.9 提供Qt Remote Objects,OAuth1 & OAuth2,重写了QML的GC
Technology Preview Modules Qt Remote Objects - A module that allows you to easily share QObject inte ...
- Linux之convert命令【转】
本文转载自:http://zlb1986.iteye.com/blog/778054 转载: 强大的convert命令 convert命令可以用来转换图像的格式,支持JPG, BMP, PCX, GI ...
- CNN中的局部连接(Sparse Connectivity)和权值共享
局部连接与权值共享 下图是一个很经典的图示,左边是全连接,右边是局部连接. 对于一个1000 × 1000的输入图像而言,如果下一个隐藏层的神经元数目为10^6个,采用全连接则有1000 × 1000 ...
- UVa 11722(几何概率)
题意:你和你的朋友要乘坐火车,并且都会在A城市相遇,你会在(t1,t2)中的任意时刻以相同的概率密度到达, 你朋友会在(s1,s2)中的任意时刻以相同的概率密度到达,你们的火车在A城市都会停留w分钟, ...
- JavaScript:DOM对象
ylbtech-JavaScript:DOM对象 1. HTML DOM Document 对象返回顶部 1. HTML DOM Document 对象 HTML DOM 节点 在 HTML DOM ...
- Wannafly挑战赛25 B 面积并 数学
题面 题意:有一个正n边形,它的外接圆的圆心位于原点,半径为l .以原点为圆心,r为半径作一个圆,求圆和这个正n边形的面积并.3<=n<=1e8 1<=l<=1e6 0< ...
- c语言open()介绍
2013-09-0914:40:13 1. 头文件: #include <sys/types.h> #include <sys/stat.h> #include <fcn ...
- 学习java的方式