急求解决方案:HTTP/1.1 400 Bad Request

evoloyeu 2010-08-09 11:39:39
我写了一个http请求的程序,从网上下载一个m3u8的文件。但是不知道为啥总是返回HTTP/1.1 400 Bad Request,求高人指教!!!

返回数据:

HTTP/1.1 400 Bad Request
Date: Mon, 09 Aug 2010 03:27:43 GMT
Server: Apache/2.2.3 (CentOS)
Content-Length: 315
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at http://live.tvmining.com Port 80</address>
</body></html>

程序代码:
static int main(){
SOCKET sSocket = INVALID_SOCKET;
SOCKADDR_IN stSvrAddrIn = {0}; /* 服务器端地址 */
char sndBuf[1024] = {0};
char rcvBuf[1024*200] = {0};
char *pRcv = rcvBuf;
int num = 0;
int nRet = SOCKET_ERROR;

WSADATA wsaData;

int tmplen = 348;

FILE* fp = fopen("D:\\vs2005\\httpRequest\\debug\\http.dat", "wb");

if( fp == NULL )
return -1;

/* HTTP 消息构造开始,这是程序的关键之处 */
sprintf(sndBuf, "GET /m3u8/assign/12345678_20100722_174735_ShenZhenTV_256000.m3u8 HTTP/1.1\r\n");
strcat(sndBuf, "Host: http://live.tvmining.com\n\r\n");

#ifdef WIN32
{
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR)
{
perror("WSAStartup");
return -1;
}
}
#endif

stSvrAddrIn.sin_family = AF_INET;
stSvrAddrIn.sin_port = htons(80);
stSvrAddrIn.sin_addr.s_addr = inet_addr("122.193.143.112");

sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

/* 连接 */
nRet = connect(sSocket, (SOCKADDR*)&stSvrAddrIn, sizeof(SOCKADDR));
if (SOCKET_ERROR == nRet)
{
printf("connect fail!\n");
goto GOEND;
}

/* 发送HTTP请求消息 */
send(sSocket, (char*)sndBuf, sizeof(sndBuf), 0);

/* 接收HTTP响应消息 */
while(1)
{
num = recv(sSocket, rcvBuf, 2048, 0);

if((0 == num) || (-1 == num))
{
break ;
}
fwrite(rcvBuf, 1, num, fp);

}


GOEND:
#ifdef WIN32
WSACleanup();
#endif

fclose(fp);

return 0;
}
...全文
41104 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
evoloyeu 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]

楼上的问题怎么解决的啊
[/Quote]

7楼是解决方案
zhanglinyuan 2012-09-05
  • 打赏
  • 举报
回复
楼上的问题怎么解决的啊
htloveww 2012-07-31
  • 打赏
  • 举报
回复
每次来都有收获,天天学习
康江峰 2012-06-06
  • 打赏
  • 举报
回复
http://classicasp.aspfaq.com/general/why-do-i-get-http/1-1-400-bad-request-errors.html
lrn0409 2011-07-18
  • 打赏
  • 举报
回复
非常感谢,我的GET 也是少了\r\n,加了后终于可以了。
ericsue 2011-02-15
  • 打赏
  • 举报
回复
没有http
sigmac 2010-10-27
  • 打赏
  • 举报
回复
我下载MMS,总是返回“HTTP/1.1 400 Bad Request [ZTEOSE]”,是不是构造有误啊?
我直接 GET HTTP://221.179.185.231/abcdef HTTP/1.1\r\n
sushy001 2010-10-25
  • 打赏
  • 举报
回复
看看这个帖子的说明
evoloyeu 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hslinux 的回复:]

问题解决了就记得顺手结贴呗,,
[/Quote]
现在结贴哈,刚才吃饭去了,多写各位。
hslinux 2010-08-09
  • 打赏
  • 举报
回复
问题解决了就记得顺手结贴呗,,
evoloyeu 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hqin6 的回复:]

Your browser sent a request that this server could not understand


sprintf(sndBuf, "GET /m3u8/assign/12345678_20100722_174735_ShenZhenTV_256000.m3u8 HTTP/1.1\r\n");
strcat(sndBuf, "Host: http:……
[/Quote]

搞定了,多谢了。
太乙 2010-08-09
  • 打赏
  • 举报
回复
Your browser sent a request that this server could not understand


sprintf(sndBuf, "GET /m3u8/assign/12345678_20100722_174735_ShenZhenTV_256000.m3u8 HTTP/1.1\r\n");
strcat(sndBuf, "Host: http://live.tvmining.com\r\n\r\n");
xixiaoliu 2010-08-09
  • 打赏
  • 举报
回复
Host: live.tvmining.com:80\r\n
xixiaoliu 2010-08-09
  • 打赏
  • 举报
回复
1,host 这里去掉 http://
evoloyeu 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ayw215 的回复:]

貌似你的REQUEST没构造好。。。
[/Quote]
没看出来有什么问题啊。。。
ayw215 2010-08-09
  • 打赏
  • 举报
回复
貌似你的REQUEST没构造好。。。
evoloyeu 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ayw215 的回复:]

static int main(){
===
你确定你写的是c 么。。
[/Quote]
没问题的哈。
ayw215 2010-08-09
  • 打赏
  • 举报
回复
static int main(){
===
你确定你写的是c 么。。

70,038

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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