2017-11-11 Sa Oct Is it online

9:07 AM

After breakfast I tried connecting to the course selection system (CSS) however got only a 502 Bad Gateway. Since the CSS is usually put online before when it was said, so it came up to my mind: Why not write a program to detect whether it is online automatically? I have become willing to learn the Network Programming since a long time before.

I meant 'Network Programming', so I'm not just using libraries like urlllib and won't use Python.

And I went to this page: Simple C example of doing an HTTP POST and consuming the response.

asked 3 years, 8 months ago, http://stackoverflow.com/questions/22077802/simple-c-example-of-doing-an-http-post-and-consuming-the-response

Here is the answer of Jerry Jeremiah, answered Mar 3 '14 at 0:54. (https://stackoverflow.com/users/2193968/jerry-jeremiah)

A message has a header part and a message body separated by a blank line. The blank line is ALWAYS needed even if there is no message body. The header starts with a command and has additional lines of key value pairs separated by a colon and a space. If there is a message body, it can be anything you want it to be.

Lines in the header and the blank line at the end of the header must end with a carraige return and linefeed pair (see HTTP header line break style) so that's why those lines have \r\n at the end.

https://stackoverflow.com/questions/5757290/http-header-line-break-style

A URL has the form of http://host:port/path?query_string

There are two main ways of submitting a request to a website:

GET: The query string is optional but, if specified, must be reasonably short. Because of this the header could just be the GET command and nothing else. A sample message could be:

GET /path?query_string HTTP/1.0\r\n
\r\n

POST: What would normally be in the query string is in the body of the message instead. Because of this the header needs to include the Content-Type: and Content-Length: attributes as well as the POST command. A sample message could be:

POST /path HTTP/1.0\r\n
Content-Type: text/plain\r\n
Content-Length: 12\r\n
\r\n
query_string

So, to answer your question: if the URL you are interested in POSTing to is http://api.somesite.com/apikey=ARG1&command=ARG2 then there is no body or query string and, consequently, no reason to POST because there is nothing to put in the body of the message and so nothing to put in the Content-Type: and Content-Length:

I guess you could POST if you really wanted to. In that case your message would look like:

POST /apikey=ARG1&command=ARG2 HTTP/1.0\r\n
\r\n

So to send the message the C program needs to:

  • create a socket
  • lookup the IP address
  • open the socket
  • send the request
  • wait for the response
  • close the socket

The send and receive calls won't necessarily send/receive ALL the data you give them - they will return the number of bytes actually sent/received. It is up to you to call them in a loop and send/receive the remainder of the message.

What I did not do in this sample is any sort of real error checking - when something fails I just exit the program. Let me know if it works for you:

#include <stdio.h> /* printf, sprintf */
#include <stdlib.h> /* exit */
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */ void error(const char *msg) { perror(msg); exit(0); } int main(int argc,char *argv[])
{
/* first what are we going to send and where are we going to send it? */
int portno = 80;
char *host = "api.somesite.com";
char *message_fmt = "POST /apikey=%s&command=%s HTTP/1.0\r\n\r\n"; struct hostent *server;
struct sockaddr_in serv_addr;
int sockfd, bytes, sent, received, total;
char message[1024],response[4096]; if (argc < 3) { puts("Parameters: <apikey> <command>"); exit(0); } /* fill in the parameters */
sprintf(message,message_fmt,argv[1],argv[2]);
printf("Request:\n%s\n",message); /* create the socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket"); /* lookup the ip address */
server = gethostbyname(host);
if (server == NULL) error("ERROR, no such host"); /* fill in the structure */
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length); /* connect the socket */
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting"); /* send the request */
total = strlen(message);
sent = 0;
do {
bytes = write(sockfd,message+sent,total-sent);
if (bytes < 0)
error("ERROR writing message to socket");
if (bytes == 0)
break;
sent+=bytes;
} while (sent < total); /* receive the response */
memset(response,0,sizeof(response));
total = sizeof(response)-1;
received = 0;
do {
bytes = read(sockfd,response+received,total-received);
if (bytes < 0)
error("ERROR reading response from socket");
if (bytes == 0)
break;
received+=bytes;
} while (received < total); if (received == total)
error("ERROR storing complete response from socket"); /* close the socket */
close(sockfd); /* process response */
printf("Response:\n%s\n",response); return 0;
}

Like the other answer pointed out, 4096 bytes is not a very big response. I picked that number at random assuming that the response to your request would be short. If it can be big you have two choices:

  • read the Content-Length: header from the response and then dynamically allocate enough memory to hold the whole response.
  • write the response to a file as the pieces arrive

There's still some above, but that's enough for me. How to detect 502 Bad Gateway?

9:40 AM

I'll firstly test it with my blog.

C:\Users\01\Desktop>cc foo.c
C:\Users\01\AppData\Local\Temp/cc4qSwMr.o:foo.c:(.text+0xac): undefined referenc
e to `socket'
C:\Users\01\AppData\Local\Temp/cc4qSwMr.o:foo.c:(.text+0xcc): undefined referenc
e to `gethostbyname'
C:\Users\01\AppData\Local\Temp/cc4qSwMr.o:foo.c:(.text+0x110): undefined referen
ce to `htons'
C:\Users\01\AppData\Local\Temp/cc4qSwMr.o:foo.c:(.text+0x152): undefined referen
ce to `connect'
collect2: ld returned 1 exit status

libws2_32.a, libwsock32.a

C:\Users\01\Desktop>cc foo.c -l libws2_32.a -l libwsock32.a
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot fin
d -llibws2_32.a
collect2: ld returned 1 exit status C:\Users\01\Desktop>cc foo.c -l C:\MinGW\lib\libws2_32.a -l C:\MinGW\lib\libwsoc
k32.a
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot fin
d -lC:\MinGW\lib\libws2_32.a
collect2: ld returned 1 exit status C:\Users\01\Desktop>cc foo.c -l "C:\MinGW\lib\libws2_32.a" -l "C:\MinGW\lib\libw
sock32.a"
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot fin
d -lC:\MinGW\lib\libws2_32.a
collect2: ld returned 1 exit status C:\Users\01\Desktop>cc foo.c "C:\MinGW\lib\libws2_32.a" "C:\MinGW\lib\libwsock32
.a"
C:\Users\01\AppData\Local\Temp/ccENCuED.o:foo.c:(.text+0xac): undefined referenc
e to `socket'
C:\Users\01\AppData\Local\Temp/ccENCuED.o:foo.c:(.text+0xcc): undefined referenc
e to `gethostbyname'
C:\Users\01\AppData\Local\Temp/ccENCuED.o:foo.c:(.text+0x110): undefined referen
ce to `htons'
C:\Users\01\AppData\Local\Temp/ccENCuED.o:foo.c:(.text+0x152): undefined referen
ce to `connect'
collect2: ld returned 1 exit status C:\Users\01\Desktop>cc foo.c -lsocket
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot fin
d -lsocket
collect2: ld returned 1 exit status

Starting virtualbox.. Fedora-Scientific_KDE-Live-x86_64-25-1.3.iso

didn't work. BT5R3-GNOME-32.iso

It reminds me that my first contact to Linux was with Backtrack. It's about my fourth or fifth grade in the primary school, when my father brought back home a WiFiCity suite from Hong Kong, which is used for getting others' WiFi password and surf the Net with that. We did succeed but the speed is too slow. I can stil remember that when connected my father tested the network with the Luogang District Government website but I insisted on the dnf.qq.com.

2017-11-11 Sa Oct Is it online的更多相关文章

  1. 2017年11月Dyn365/CRM用户社区活动报名

    UG是全球最大Dynamics的用户组织,由最终用户自发组织,由行业有经验的专家自愿贡献知识和经验的非营利机构,与会人员本着务实中立的态度,不进行推介产品,服务以及其他营销行为.在美国,微软Dynam ...

  2. WPS 表格筛选两列相同数据-完美-2017年11月1日更新

    应用: 1.选出A列中的数据是否在B列中出现过: 2.筛选出某一批序号在一个表格里面的位置(整批找出) 3.其实还有其他很多应用,难描述出来... ... A列中有几百的名字,本人想帅选出B列中的名字 ...

  3. 2017年11月GitHub上最热门的Java项目出炉

    2017年11月GitHub上最热门的Java项目出炉~ 一起来看看这些项目你使用过哪些呢? 1分布式 RPC 服务框架 dubbohttps://github.com/alibaba/dubbo S ...

  4. 2017.11.11 B201 练习题思路及解题方法

    2017.11.11 B201 练习题思路及解题方法 题目类型及涵盖知识点 本次总共有6道题目,都属于MISC分类的题目,涵盖的知识点有 信息隐藏 暴力破解 音轨,摩斯电码 gif修改,base64原 ...

  5. 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11

    Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...

  6. 2017-11-11 Sa Oct 消参

    2017-11-11 Sa Oct 消参 Prior versions: 2017-11-04 Sa Oct 消参 2017-11-10 Fr Oct 消参 2017-11-04 Sa $ P(-3, ...

  7. NOIp 11.11/12

    最后一场比较正式的NOIp模拟赛,写一发小总结.题目没什么好说的,大部分很简单,先贴一下代码. 1111 T1 //string //by Cydiater //2016.11.11 #include ...

  8. 11.11光棍节工作心得——github/MVP

    11.11光棍节工作心得 1.根据scrum meeting thirdday中前辈的指导进行学习 我在博客中贴了链接,竟然TrackBack引来了原博主,

  9. 下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y),y++);

    下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...

  10. 2017-11-11 Sa Oct Spider

    2017-11-11 Sa Oct Spider 4:33 PM Again. Firstly test liburl: # -*- coding: utf-8 -*- import json imp ...

随机推荐

  1. 给jumpserver双机配置glusterfs共享复制卷

    为什么要使用glusterfs呢. 本身Haproxy+Keepalived对jumpserver进行了负载均衡和反向代理.但是真实的视频只会存储在一个节点上 否则播放视频的时候会出现找不到的情况 为 ...

  2. taro 报错及解决

    1.解决:taro 升级到最新版(npm install -g @tarojs/cli) 错误 组件编译 组件src/pages/xxx/xxx.tsx编译失败! TypeError: callee. ...

  3. php设置cookie为httponly防止xss攻击

    什么是XSS攻击? XSS攻击(Cross Site Scripting)中文名为跨站脚本攻击,XSS攻击时web中一种常见的漏洞.通过XSS漏洞可以伪造目标用户登录,从而获取登录后的账号操作. 网站 ...

  4. 使用Pandas将多个数据表合一

    使用Pandas将多个数据表合一 将多张数据表合为一张表,便于统计分析,进行这一操作的前提为这多张数据表互相之间有关联信息,或者有相同的列. import pandas as pd unames = ...

  5. 【C++】源自指针的报错

    最近在调试PCL程序的时候,被这个报错折腾了好久. 无数血泪史总结成一句话,指针未初始化! PointCloudXYZ::Ptr plane_ptr;   错误!!! PointCloudXYZ::P ...

  6. Ubuntu 16.04 安装Go 1.9.2

    系统环境 Ubuntu: 16.04 Go: 1.9.2 安装步骤 $ curl -O https://storage.googleapis.com/golang/go1.9.linux-amd64. ...

  7. [UE4]用Format Text进行调试

    {姓名},在{时间}进来了 “{姓名}”和“{时间}”会自动变成一个变量.

  8. 负载均衡器技术Nginx和F5的优缺点对比

    负载均衡器技术Nginx和F5的优缺点对比 博客分类: 应用服务 F5nginx  对于数据流量过大的网络中,往往单一设备无法承担,需要多台设备进行数据分流,而负载均衡器就是用来将数据分流到多台设备的 ...

  9. php预定义常量

    <?php echo "当前文件路径: ".__FILE__; echo "<br/>当前行数:".__LINE__; echo " ...

  10. window注册表相关

    参考: https://baike.baidu.com/item/REG_EXPAND_SZ/9102962 一 注册表的相关概念 windows注册表相关api中名字起的比较混乱, 在这放一张从网上 ...