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 默 ...
随机推荐
- 修改linux内核的启动logo和禁用启动光标【转】
本文转载自:http://blog.csdn.net/hunanchenxingyu/article/details/40992947 1-1.制作logo的方法: 首先选择一个自己喜欢的图片,png ...
- suse linux通过iso文件安装gcc
mount -t iso9660 -o loop SLES-11-SP4-DVD-x86_64-GM-DVD1.iso /media/#仅仅上述iso1即可 不需要mount iso2 mount - ...
- ansible usually
链接地址:https://my.oschina.net/kangvcar/blog/1830155
- WPF开发“Program '*' does not contain a static 'Main' method suitable for an entry point”错误
WPF项目编译时出现“Program '*' does not contain a static 'Main' method suitable for an entry point”错误, 解决方 ...
- 「图解HTTP 笔记」Web 基础
Web 基础 三项构建技术: HTML:页面的文本标记语言 HTTP:文档传输协议 URL:指定文档所在地址 一些概念 HTTP(HyperText Transfer Protocol):通常被译为& ...
- 模拟试题C
模拟试题C 一.单项选择题(2′*14 =28′) 1.双线性法向插值法(Phong Shading)的优点是( ) A)法向计算精确 B)高光域准确 C)对光源和视点没有限制 D)速度较快 2.用编 ...
- C#操作Oracle数据库中文乱码问题解决
最近公司有一个对外项目,采用的是oracle数据库,以前做的项目基本都是SQLserver,有和oracle对接的也就一些简单的增删查改. 还巧合的遇到乱码问题,网上各种查找,筛选,总算是把问题解决了 ...
- 页面footer在底部
页脚动态贴在底部需要满足以下两个条件: 当主体的内容高度不超过可视区域高度的时候,页脚贴在页面底部. 当主体的内容高度超过可视区域高度的时候,页脚将按正常布局. 方法一:footer高度固定+绝对定位 ...
- Robot Framework(四)创建测试套件
2.3.1测试用例文件 Robot Framework测试用例是使用测试用例文件中的测试用例表创建的.这样的文件会自动从它包含的所有测试用例中创建一个测试套件.可以有多少测试用例没有上限,但建议少于1 ...
- 团体程序设计天梯赛-练习集-L1-025. 正整数A+B
L1-025. 正整数A+B 本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空 ...