关于http请求的一个奇怪问题(不看你就不知道如何发送。。。) 用get方法是这样直接在url中传送但它用的是post传送 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 两者的区别就是,请求的方式一个是GET,一个是POST,两者都能向服务器端传递参数,但是服务器端能判断出来,客户的请求是GET或是POST。如果服务器短的程序对GET的请求方法不予支持,就没有办法通过直接输入url来完成,这要看服务器端的程序是如何编的,是不是对GET和POST都支持。不过好在不管是GET或是POST都是用TCP/IP连接,你用VC来实现,就不会有什么大的障碍了。你完全可以通过SOCKET来模拟一个GET的请求或者是POST的请求,向服务器端发出,来完成手机短消息的发送。 请问maptrix如果用socket实现可不可以给我一段代码,来模拟post或get请求,向服务器端。 用ASP写的好象是GET 和 POST 都可以用。下面是GET /POST的函数SOCKET HTTPOpen(LPCSTR Server,unsigned short port){ struct sockaddr_in sa; HOSTENT *hp; SOCKET sock=NULL; sock =socket(AF_INET,SOCK_STREAM,0); if(sock==INVALID_SOCKET ) {#ifdef _DEBUG printf("Error :Can't initialize TCP/IP socket\n");#endif return INVALID_SOCKET; } hp=gethostbyname(Server); if(!hp) {#ifdef _DEBUG printf("Error :Can't find this server :%s\n",Server);#endif return INVALID_SOCKET; } sa.sin_family = AF_INET; sa.sin_addr.s_addr = *(unsigned long*)hp->h_addr; /* Server IP */ sa.sin_port = htons(port); /* Server Port number */ if (connect(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){#ifdef _DEBUG printf("Error :Connection failed\n");#endif return INVALID_SOCKET; }#ifdef _DEBUG printf("Connected to server :%s successfully\n",Server);#endif return sock;}//static char HTTP_Request[4096];int HTTPGet(SOCKET sock,LPCSTR Resource){ char HTTP_Request[4096]; int length; if(strlen(Resource)>4000) return 0; sprintf(HTTP_Request,"GET %s HTTP/1.0\r\n\r\n",Resource); length=strlen(HTTP_Request);#ifdef _DEBUG printf("Send GET Request:\n%s",HTTP_Request);#endif return SockSendAll(sock,HTTP_Request,length);}void HTTPClose(SOCKET sock){ closesocket(sock);}#define HTTP_POST_FORMAT "POST %s HTTP/1.0\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*\r\nReferer: http://%s/\r\nAccept-Language: en-us,zh-cn;q=0.8,zh-tw;q=0.5,zh-hk;q=0.3\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\nHost: %s\r\nContent-Length: %u\r\n\r\n%s"int HTTPPost (SOCKET sock,LPCSTR Server,LPCSTR Resource,LPCSTR PostInfo){ char HTTP_Request[4096]; if(strlen(Resource)+strlen(PostInfo)>4000) return 0; sprintf(HTTP_Request,HTTP_POST_FORMAT,Resource,Server,Server,strlen(PostInfo),PostInfo);#ifdef _DEBUG printf("Send POST Request:%s",HTTP_Request);#endif return SockSendAll(sock,HTTP_Request,strlen(HTTP_Request));} Kevin_qing是正确的,如果你请求一个GET,只需要将参数Resource设为"cgi-bin/gsm.cgi?sender=testusr&phone_no=testphoneno&content=hello"调用HTTPGet函数,若请求一个POST,需将参数Resource设为"cgi-bin/gsm.cgi"将参数PostInfo设为"sender=testusr&phone_no=testphoneno&content=hello"调用HTTPPost函数 对了,要使用上面的函数,还需要定义一个全局变量char *Server="mail.135-139.com";//服务器的地址 我试着写了一个程序来完成此功能,发送post请求成功(server:211.103.80.163 port:80)将如下内容送入socket sendbufPOST /cgi-bin/gsm.cgi HTTP/1.0Accept:image/gif,image/x-xbitamp,image/jpeg,image/pjpeg,*/*Referer:http://mail.135-139.com/gsm_default.htmAccept-Language:en-us,zh-cn;q=0.8,zh-tw;q=0.5,zh-hk;q=0.3Content-Type:application/x-www-form-urlencodedUser-Agent:Mozilla/4.0 (compatible;MSIE 5.01;Window NT 5.0)Host:211.103.80.163Content-Length:44sender=usr&phone_no=myphoneno&content=test(myphoneno is my phone no)成功接收如下信息。HTTP/1.1 200 OKServer: Netscape-Enterprise/3.6Date: Wed, 06 Sep 2000 04:48:16 GMTContent-type: text/html但我的手机还是收不到任何信息各位请再帮我找一找原因,稍后给分,谢谢。 你现在通信的程序已经没有问题了,现在就是和服务器的接口是不是正确还很难说,我写了下面的代码,在IE里经过提交后,返回“手机短消息发送失败!”的消息。所以你看看你和服务器的接口是正确吗?是只需要提交sender、phone_no、content就可以完成了吗?你可以再仔细看看原来叶面的的代码是怎么写的,然后改写下面的代码进行实验,祝你早日成功!<html><body><form method=POST action="http://211.103.80.163/cgi-bin/gsm.cgi"><input name=sender value="usr"><input name=phone_no value="13501111111"> #号码用的是我的号码,这个是随便的号码<input name=content value="test"><input type=submit value="go"></form></body></html> to matrix & pipu:请注意参数的大小写应该是SENDERPHONE_NOCONTENT你们两都写錯了 对,我看了一下主页源文件,发现提交的元素名称都是大写,只要用下面的代码就可以成功!<html><body><form method=POST action="http://211.103.80.163/cgi-bin/gsm.cgi"><input name=SENDER value="usr"><input name=PHONE_NO value="135011111111"><input name=CONTENT value="test"><input type=submit value="go"></form></body></html> OnBeforeNavigate2事件中pDisp跳转URL问题 MFC中的ActiveX空间能播放哪些格式的视频文件? 谁有GetPrivateProfileString 、WritePrivateProfileString的实现的代码? 文件问题,请大家帮助!(文件结尾问题) 请问如何在BMP图象某一像素点位置写字! 关于polyimage library 谁用过这个东东阿 想问一些视频方面入门级的经典书? 高分求助 打开不了dll中的对话框,如何处理 快来!!!!!!!!!! 关于boot loader的问题,很简单的呀? 对话框热键
如果服务器短的程序对GET的请求方法不予支持,就没有办法通过直接输入url来完成,这要看服务器端的程序是如何编的,是不是对GET和POST都支持。
不过好在不管是GET或是POST都是用TCP/IP连接,你用VC来实现,就不会有什么大的障碍了。你完全可以通过SOCKET来模拟一个GET的请求或者是POST的请求,向服务器端发出,来完成手机短消息的发送。
下面是GET /POST的函数
SOCKET HTTPOpen(LPCSTR Server,unsigned short port)
{
struct sockaddr_in sa;
HOSTENT *hp;
SOCKET sock=NULL; sock =socket(AF_INET,SOCK_STREAM,0);
if(sock==INVALID_SOCKET ) {
#ifdef _DEBUG
printf("Error :Can't initialize TCP/IP socket\n");
#endif
return INVALID_SOCKET;
} hp=gethostbyname(Server);
if(!hp) {
#ifdef _DEBUG
printf("Error :Can't find this server :%s\n",Server);
#endif
return INVALID_SOCKET;
} sa.sin_family = AF_INET;
sa.sin_addr.s_addr = *(unsigned long*)hp->h_addr; /* Server IP */
sa.sin_port = htons(port); /* Server Port number */ if (connect(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){
#ifdef _DEBUG
printf("Error :Connection failed\n");
#endif
return INVALID_SOCKET;
}
#ifdef _DEBUG
printf("Connected to server :%s successfully\n",Server);
#endif
return sock;
}//static char HTTP_Request[4096];int HTTPGet(SOCKET sock,LPCSTR Resource)
{
char HTTP_Request[4096];
int length;
if(strlen(Resource)>4000)
return 0;
sprintf(HTTP_Request,"GET %s HTTP/1.0\r\n\r\n",Resource);
length=strlen(HTTP_Request);
#ifdef _DEBUG
printf("Send GET Request:\n%s",HTTP_Request);
#endif
return SockSendAll(sock,HTTP_Request,length);
}void HTTPClose(SOCKET sock)
{
closesocket(sock);
}
#define HTTP_POST_FORMAT "POST %s HTTP/1.0\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*\r\nReferer: http://%s/\r\nAccept-Language: en-us,zh-cn;q=0.8,zh-tw;q=0.5,zh-hk;q=0.3\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\nHost: %s\r\nContent-Length: %u\r\n\r\n%s"int HTTPPost (SOCKET sock,LPCSTR Server,LPCSTR Resource,LPCSTR PostInfo)
{
char HTTP_Request[4096];
if(strlen(Resource)+strlen(PostInfo)>4000)
return 0;
sprintf(HTTP_Request,HTTP_POST_FORMAT,Resource,Server,Server,strlen(PostInfo),PostInfo);
#ifdef _DEBUG
printf("Send POST Request:%s",HTTP_Request);
#endif return SockSendAll(sock,HTTP_Request,strlen(HTTP_Request));
}
"cgi-bin/gsm.cgi?sender=testusr&phone_no=testphoneno&content=hello"
调用HTTPGet函数,
若请求一个POST,需将参数Resource设为
"cgi-bin/gsm.cgi"
将参数PostInfo设为
"sender=testusr&phone_no=testphoneno&content=hello"
调用HTTPPost函数
char *Server="mail.135-139.com";//服务器的地址
将如下内容送入socket sendbuf
POST /cgi-bin/gsm.cgi HTTP/1.0
Accept:image/gif,image/x-xbitamp,image/jpeg,image/pjpeg,*/*
Referer:http://mail.135-139.com/gsm_default.htm
Accept-Language:en-us,zh-cn;q=0.8,zh-tw;q=0.5,zh-hk;q=0.3
Content-Type:application/x-www-form-urlencoded
User-Agent:Mozilla/4.0 (compatible;MSIE 5.01;Window NT 5.0)
Host:211.103.80.163
Content-Length:44sender=usr&phone_no=myphoneno&content=test(myphoneno is my phone no)
成功接收如下信息。
HTTP/1.1 200 OK
Server: Netscape-Enterprise/3.6
Date: Wed, 06 Sep 2000 04:48:16 GMT
Content-type: text/html
但我的手机还是收不到任何信息各位请再帮我找一找原因,稍后给分,谢谢。
<body>
<form method=POST action="http://211.103.80.163/cgi-bin/gsm.cgi">
<input name=sender value="usr">
<input name=phone_no value="13501111111"> #号码用的是我的号码,这个是随便的号码
<input name=content value="test">
<input type=submit value="go">
</form>
</body>
</html>
请注意参数的大小写
应该是
SENDER
PHONE_NO
CONTENT
你们两都写錯了
<html>
<body>
<form method=POST action="http://211.103.80.163/cgi-bin/gsm.cgi">
<input name=SENDER value="usr">
<input name=PHONE_NO value="135011111111">
<input name=CONTENT value="test">
<input type=submit value="go">
</form>
</body>
</html>