示例程序,转载自CNBLOG,做了针对C语言编译器的适应性修正:
#include <stdio.h>#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") /* WinSock使用的库函数 */
/* 定义常量 */
#define HTTP_DEF_PORT 80 /* 连接的缺省端口 */
#define HTTP_BUF_SIZE 1024 /* 缓冲区的大小 */
#define HTTP_HOST_LEN 256 /* 主机名长度 */
char *http_req_hdr_tmpl = "GET %s HTTP/1.1\r\n"
"Accept: image/gif, image/jpeg, */*\r\nAccept-Language: zh-cn\r\n"
"Accept-Encoding: gzip, deflate\r\nHost: %s:%d\r\n"
"User-Agent: Huiyong's Browser <0.1>\r\nConnection: Keep-Alive\r\n\r\n"
/**************************************************************************
*
* 函数功能: 解析命令行参数, 分别得到主机名, 端口号和文件名. 命令行格式:
* [http://www.baidu.com:8080/index.html]
*
* 参数说明: [IN] buf, 字符串指针数组
* [OUT] host, 保存主机
* [OUT] port, 端口
* [OUT] file_name, 文件名
*
* 返 回 值: void.
*
**************************************************************************/
void http_parse_request_url(const char *buf, char *host,
unsigned short *port, char *file_name)
{
int length = 0
char port_buf[8]
char *buf_end = (char *)(buf + strlen(buf))
char *begin, *host_end, *colon, *file
/* 查找主机的开始位置 */
begin = (char*)(strstr(buf, "//"))
begin = (begin ? begin + 2 : (char*)(buf))
colon = strchr(begin, ':')
host_end = strchr(begin, '/')
if (host_end == NULL)
{
host_end = buf_end
}
else
{ /* 得到文件名 */
file = strrchr(host_end, '/')
if (file && (file + 1) != buf_end)
strcpy(file_name, file + 1)
}
if (colon) /* 得到端口号 */
{
colon++
length = host_end - colon
memcpy(port_buf, colon, length)
port_buf[length] = 0
*port = atoi(port_buf)
host_end = colon - 1
}
/* 得到主机信息 */
length = host_end - begin
memcpy(host, begin, length)
host[length] = 0
}
int main(int argc, char **argv)
{
WSADATA wsa_data
SOCKET http_sock = 0 /* socket 句柄 */
struct sockaddr_in serv_addr /* 服务器地址 */
struct hostent *host_ent
int result = 0, send_len
char data_buf[HTTP_BUF_SIZE]
char host[HTTP_HOST_LEN] = "127.0.0.1"
unsigned short port = HTTP_DEF_PORT
unsigned long addr
char file_name[HTTP_HOST_LEN] = "index.html"
char file_nameforsave[HTTP_HOST_LEN] = "index1.html"
FILE *file_web
if (argc != 2)
{
printf("[Web] input : %s http://www.test.com[:8080]/index.html", argv[0])
return -1
}
http_parse_request_url(argv[1], host, &port, file_name)
WSAStartup(MAKEWORD(2,0), &wsa_data) /* 初始化 WinSock 资源 */
addr = inet_addr(host)
if (addr == INADDR_NONE)
{
host_ent = gethostbyname(host)
if (!host_ent)
{
printf("[Web] invalid host\n")
return -1
}
memcpy(&addr, host_ent->h_addr_list[0], host_ent->h_length)
}
/* 服务器地址 */
serv_addr.sin_family = AF_INET
serv_addr.sin_port = htons(port)
serv_addr.sin_addr.s_addr = addr
http_sock = socket(AF_INET, SOCK_STREAM, 0) /* 创建 socket */
result = connect(http_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
if (result == SOCKET_ERROR) /* 连接失败 */
{
closesocket(http_sock)
printf("[Web] fail to connect, error = %d\n", WSAGetLastError())
return -1
}
/* 发送 HTTP 请求 */
send_len = sprintf(data_buf, http_req_hdr_tmpl, argv[1], host, port)
result = send(http_sock, data_buf, send_len, 0)
if (result == SOCKET_ERROR) /* 发送失败 */
{
printf("[Web] fail to send, error = %d\n", WSAGetLastError())
return -1
}
file_web = fopen(file_nameforsave, "a+")
do /* 接收响应并保存到文件中 */
{
result = recv(http_sock, data_buf, HTTP_BUF_SIZE, 0)
if (result > 0)
{
fwrite(data_buf, 1, result, file_web)
/* 在屏幕上输出 */
data_buf[result] = 0
printf("%s", data_buf)
}
} while(result > 0)
fclose(file_web)
closesocket(http_sock)
WSACleanup()
return 0
}
根据软糖的经验,[API] RegisterHotKey似乎只能在本机注册热键。
如果简简单单就能通过网页调用对方电脑的Windows API,那黑客也不用黑了。
直接调用对方的API,想删文件删文件,想关机就关机。
至于如何实现为网页添加快捷键,偶也不会,逃~~~(
C语言发post请求数据程序, 工作需要,网上查资料N篇,作为半路出家学编程的,走过了N个坑,终于完成以下的测试程序。
使用了curl的库, 这样无论在windows或者在linux都可以使用.
win下的编程环境是TDM-GCC-64, 怎样安装,也是另一个话题。需要这个的请自行上网查询怎样安装.
linux 下是gcc环境,最好先安装curl开发包,目的就是需要curl.h等文件, 怎样安装,也是另一个话题。
废话不说,以下是正式程序.
#include
#include
#include
#include
struct string {
char *ptr
size_t len
}
void init_string(struct string *s) {
s->len = 0
s->ptr = malloc(s->len + 1)
if (s->ptr == NULL) {
fprintf(stderr, "malloc() failed ")
exit(EXIT_FAILURE)
}
s->ptr[0] = ''
}
size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
size_t new_len = s->len + size * nmemb
s->ptr = realloc(s->ptr, new_len + 1)
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed ")
exit(EXIT_FAILURE)
}
memcpy(s->ptr + s->len, ptr, size*nmemb)
s->ptr[new_len] = ''
s->len = new_len
return size * nmemb
}
CURLcode curl_post_req(char *url, char *postParams,struct curl_slist *headers, char *response)
{
CURL *curl
curl = curl_easy_init()//初始化
// curl返回值
CURLcode res
if (curl)
{
struct string s
init_string(&s)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers)
//curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/post")
curl_easy_setopt(curl, CURLOPT_URL, url)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s)
curl_easy_setopt(curl, CURLOPT_POST, 1)//设置CURLOPT_POST之后必须带有POST数据
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams)
//不接收响应头数据0代表不接收 1代表接收
curl_easy_setopt(curl, CURLOPT_HEADER, 0)
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L)
//CURLOPT_VERBOSE的值为1时,会显示详细的调试信息
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0)
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL)
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1)
//设置超时时间,以秒来计算 CURLOPT_CONNECTTIMEOUT是连接超时
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10)
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10)
// https ssl 时需要用到,如果是 http 可以注释掉
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L)
res = curl_easy_perform(curl)
//printf(" =>%s=>=>", s.ptr)
sprintf(response," =>%s ", s.ptr)
free(s.ptr)
curl_slist_free_all(headers)
}
curl_easy_cleanup(curl)
return res
}
int main(int argc, const char *argv[])
{
// http 请求头, 构造
printf(" start... ")
struct curl_slist *headers1 = NULL
headers1 = curl_slist_append(headers1, "User-Agent: Mozilla/5.0 (Windows NT 10.0WOW64Trident/7.0rv:11.0) like Gecko")
headers1 = curl_slist_append(headers1, "Content-Type:application/x-www-form-urlencodedcharset=UTF-8")
char url_post0[100] = "https://www.lpfrx.com/"
// 查找的字符串 : delphi
char paramsLogin0[100] = "s=delphi"
char resPost0[40960] = ""
CURLcode res3 = curl_post_req(url_post0, paramsLogin0, headers1,resPost0)
if (res3 == CURLE_OK)
{
printf("data: %s" ,resPost0)
}
printf(" end... ")
return 0
}
// win 下cmd下运行乱码,请先执行 chcp 65001 转成 utf8. 默认是chcp 936
// win: 运行目录下需要zlib.dll libcurl.dll
// curl-Library 是我自己的目录,放在当前的程序目录下,win下的curl.h 也是需要自己去找,如果有python编程环境的话,也安装了curl库的话,应该可能会有curl.h的库路径
// win: gcc -o curlpostlpfrx curlpost_lpfrx.c -I ./curl-Library/include -L ./curl-Library/lib -lcurl
//linux: gcc -o curlpostlpfrx curlpost_lpfrx.c -lcurl
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)