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协议报头信息和主体鉴别的更多相关文章
- HTTP 协议 -- 报头信息
报头 每一个报头都是由 [名称 + ":" + 空格 + 值 + <CR><LF>] 组成.有四种不同类型的报头: 通用报头:可用于请求,也可以用于响应,它 ...
- http协议报头详解HTTP协议结构
http协议 请求报文和响应报文都是由以下4部分组成 1.请求行 2.请求头 3.空行 4.消息主体 下图为http请求的报文结构 下图为http响应报文结构 请求行 格式为: Method Requ ...
- HTTP协议请求信息详解
通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息.客户端向服务器发送一个请求,请求头包含请求的方法.URI.协议版本.以及包含请求修饰符.客户信息和内容的类似于MIME的消息结构 ...
- PHP HTTP协议(报头/状态码/缓存)
一.HTTP协议介绍 1. #HTTP协议 # (1 建立在TCP/IP协议基础上 # (2 web开发数据传输依赖于http协议 # (3 http 协议全称是文 ...
- http协议报头详解
目录: 1. http协议简介 2. http报头举例 3. http报头详解 4. 几个字段的说明 5. 总结 6. 参考文章 1. http协议简介 HTTP是Hyper Text Transfe ...
- python网络编程——使用UDP、TCP协议收发信息
UDP UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,所以可以实现广播发送. UDP传输数据时有大小限制,每个被传输的数据报必须限定在64KB之内. UDP ...
- Linux--网络基础(概念+协议的了解+OSI七层模型,TCP/IP五层协议,网络数据传输流程)
网络的发展 网络的发展有下面几个阶段: 独立模式:计算机最开始是以单机模式被广泛使用的.每一台计算机都是独立的,之间不能够进行数据共享与通信 网络互联: 计算机之间可以链接在一起,完成数据共享,计算机 ...
- javaweb学习总结十九(http协议概述以及http请求信息分析)
一:http协议概述 1:http协议定义 2:使用telnet程序连接web服务器,获取资源 3:http协议:超文本传输协议,它是基于tcp/ip协议,就是建立在tcp/ip之上工作的, http ...
- HTTP超文本传输协议-HTTP/1.1中文版
摘要 超文本传输协议(HTTP)是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议.它是一种通用的,不分状态(stateless)的协议,除了诸如名称服务和分布对象管理系统之类的超文本用途外 ...
随机推荐
- EasyUI实战经验总结(转)
最近公司培训EasyUI,就做下总结吧,给有需要的人,源码在文章最后. 1.最常用的表格 ? 1 2 3 <div class="easyui-panel" data-opt ...
- 开销是有益的:AppCan 至HTML5移动创新和创业精神和健康
2014年移动创业更趋向理性,消费级App市场接近饱和,BAT等巨头的竞争更加激烈,市场版图及格局基本定型.而企业级移动应用却迎来爆发增长,替代进入红海的消费级App市场,企业级定制APP开发成为 ...
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
L - Minimum Sum LCM Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- 声明式编程思想和EEPlat
声明式编程定义 声明式编程(英语:Declarativeprogramming)它是一种编程范式.程相对立.它描写叙述目目标性质,让计算机明白目标,而非流程. 声明式编程不用告诉电脑问题领域.从而避免 ...
- ZOJ 3820 2014ACM/ICPC牡丹江司B称号
3797714 2014 - 10 - 12 21:58 : 19 Accepted 3820 C++ 1350 70240 zz_1215 比較麻烦的一道题吧,開始的时候不停的段异常,后面知道是爆栈 ...
- Struts2 拦截器—拦截action
对于拦截器的基本使用这里我就懒得打字了,我这里就讲下如何用 Struts2 拦截器 拦截action.这是我个人的想法,如果有什么不对的,或者你们有什么更好的方法.请多多留言! 拦截器的默认拦截的方法 ...
- java--照片和BYTE这些东西阵列
使用java,图像被变换成BYTE排列.和该阵列为图象,远程传输的图片进行 参考:http://blog.csdn.net/huang9012/article/details/18241539 代码例 ...
- Jquery在线咨询地址
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=&quo ...
- android visible invisible和gone差异
android中UI应用的开发中常常会使用view.setVisibility()来设置控件的可见性.当中该函数有3个可选值.他们有着不同的含义: View.VISIBLE--->可见 View ...
- Error opening zip file or JAR manifest missing : D:\play-1.2.5/framework/play-1.2.5.jar
play框架写的项目,在eclipse中导入.build-path中全部jar包都加入.执行程序,出现: Error occurred during initialization of VM agen ...