求解referer

comp226 2000-02-19 04:18:00
在Http协议中有一个referer的字段也许叫HTTP_REFERER的变量。不知道谁知道怎样传送refer字段和url请求到web服务器。最好有winsock或wininet的源程序。
...全文
273 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
DOU 2000-02-23
  • 打赏
  • 举报
回复
Steps in a Typical HTTP Client Application

The following table shows the steps you might perform in a typical HTTP client application:

1. Begin an HTTP session. Create a CInternetSession object. Initializes WinInet and connects to server.
2. Connect to an HTTP server. UseCInternetSession::GetHttpConnection. Returns a CHttpConnection object.
3.Open an HTTP request. Use CHttpConnection::OpenRequest. Returns a CHttpFile object.
4.Send an HTTP request. Use CHttpFile::AddRequestHeaders and CHttpFile::SendRequest. Finds the file. Returns FALSE if the file is not found.
5.Read from the file. UseCHttpFile. Reads the specified number of bytes using a buffer you supply.
6.Handle exceptions. Use theCInternetException class. Handles all common Internet exception types.
7.End the HTTP session. Dispose of theCInternetSession object. Automatically cleans up open file handles and connections.

vc6中有一个例子TEAR: "Tearing" HTML Pages Off the Internet,它是Console形式的,我已试过,很好。对理解整个http操作很好帮助。运行时要带参数,如:
TEAR http://www.sina.com.cn/,会在DOS窗口中打印出所取到的文本内容。该例子使用了HttpAddRequestHeaders,正合你的需要,只不过,你把
const TCHAR szHeaders[] =_T("Accept: text/*\r\nUser-Agent: MFC_Tear_Sample\r\n");
改成
const TCHAR szHeaders[] =_T("Accept: text/*\r\nUser-Agent: MFC_Tear_Sample\r\nReferer: http://you.want.com/somepage.htm\r\n");
即可达到你的准确目标

更详细信息参见MSDN
comp226 2000-02-22
  • 打赏
  • 举报
回复
高手!!!不知道有没有sample。
另外!不知道Winsock中我该如何实现http request这一步。
DOU 2000-02-20
  • 打赏
  • 举报
回复
Referer:是由浏览器负责传送给web服务器的,不是由url输入的。举个简单的例子,如果你从http://www.some.com/some.html中点击http://www.some.com/another.html链接
访问another.html页,在服务器收到对another.html的请求时,会收到浏览器发给服务器的请求头,其中Referer:请求头的值是http://www.some.com/some.html。

如果你是用http协议直接同web服务器通信,相当于你在写浏览器,wininet提供了一组函数,可以使用HttpAddRequestHeaders发请求头:

Win32 Internet API Functions中的一组HTTP Functions

The HTTP functions described in this section control the transmission and content of HTTP requests.

HttpAddRequestHeaders : Adds one or more HTTP request headers to the HTTP request handle.
HttpEndRequest : Ends an HTTP request that was initiated by HttpSendRequestEx.
HttpOpenRequest : Creates an HTTP request handle.
HttpQueryInfo : Retrieves header information associated with an HTTP request.
HttpSendRequest : Sends the specified request to the HTTP server.
HttpSendRequestEx : Sends the specified request to the HTTP server and allows chunked transfers.
也可使用CHttpConnection,CHttpFile等对http functions的封装类(vc中有)来编程,(AddRequestHeaders是CHttpFile的成员函数),
大致流程是先建立连接,获得请求handle,使用AddRequestHeaders函数对handle添加请求头,再使用HttpSendRequest函数真正发送请求,具体函数用法请参见函数说明
我也没有这方面的例子,但大致是这样。
comp226 2000-02-20
  • 打赏
  • 举报
回复
非常高兴得到您的回答,但是我不明白
Referer: http://www.w3.org/hypertext/DataSources/Overview.html是怎样使用的。是直接在Url中输入的吗?
另外,您有相关的winsock 或 wininet例子程序吗?
DOU 2000-02-19
  • 打赏
  • 举报
回复
Referer:是一HTTP请求头,它是一个可选头域,意味着要告诉服务器文档的URL,所请求的URL就是从这个文档中得到的。它同Accept:,Accept-Encoding:,User-Agent:等请求头的使用方法相同,传送格式如例:
Referer: http://www.w3.org/hypertext/DataSources/Overview.html

4,354

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧