如何获取HTML页面使用套接字 [英] how to fetch a html page use socket

查看:86
本文介绍了如何获取HTML页面使用套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我使用套接字获取html页面

和请求标签"Accept-Encoding:gzip",因此我收到了压缩数据

我尝试使用zlib解压缩,但是调用函数inflate
时失败
我使用以下代码:

hi all

i use socket to fetch the html page

and the request tag "Accept-Encoding:gzip" so i recieve a compress data

i try use zlib to uncompress but i failed when i call function inflate

i use the code follow:

#define CHUNK 16384
int inflate_read(char *source,int len,char **dest,int gzip) 
{ 
	int ret; 
	unsigned have; 
	z_stream strm; 
	unsigned char out[CHUNK] = {0}; 
	int totalsize = 0; 

	/* allocate inflate state */ 
	strm.zalloc = Z_NULL; 
	strm.zfree = Z_NULL; 
	strm.opaque = Z_NULL; 
	strm.avail_in = 0; 
	strm.next_in = Z_NULL; 

	if(gzip) 
		ret = inflateInit2(&strm, 47); 
	else 
		ret = inflateInit(&strm); 

	if (ret != Z_OK) 
		return ret; 

	strm.avail_in = len; 
	strm.next_in = (Bytef*)source; 

	/* run inflate() on input until output buffer not full */ 
	do { 
		strm.avail_out = CHUNK; 
		strm.next_out = out; 
		ret = inflate(&strm, Z_NO_FLUSH); 
		assert(ret != Z_STREAM_ERROR); /* state not clobbered */ 
		switch (ret) { 
		case Z_NEED_DICT: 
			ret = Z_DATA_ERROR; /* and fall through */ 
		case Z_DATA_ERROR: 
		case Z_MEM_ERROR: 
			inflateEnd(&strm); 
			return ret; 
		} 
		have = CHUNK - strm.avail_out; 
		totalsize += have; 
		*dest = (char*)realloc(*dest,totalsize); 
		memcpy(*dest + totalsize - have,out,have); 
	} while (strm.avail_out == 0); 

	/* clean up and return */ 
	(void)inflateEnd(&strm); 
	return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; 
} 




有人可以帮我解压缩数据,并使用"Transfer-Encoding:chunked"解压缩数据

谢谢




someone can help me how to uncompress data and uncompress data with "Transfer-Encoding: chunked"

thank you

推荐答案

此代码获取html页面.将html页面的地址而不是http://example.com提取.下面发布的代码来自 http://curl.haxx.se/libcurl/c/simple.html [^ ]

This code gets html page. Put html page''s address to fetch instead of http://example.com. Below posted code is from http://curl.haxx.se/libcurl/c/simple.html[^]

#include <stdio.h>
#include 
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    res = curl_easy_perform(curl);
 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}
Y</stdio.h>


这篇关于如何获取HTML页面使用套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆