通过http协议下载MP3的关键就是 整块打包,一块一块向文件里面存储。读取的时候用二进制
/***
szWebAddr: 页面地址(包含host+addr)
szMp3FileName:将要存储文件的名字
szRequest: 请求内容
szHeadEx: http头附加信息(替换其默认的设置)
iReqType: 请求类型(1--Get;2--Post)
iTimeout: 超时时间(秒)
返回: 页面返回数据
**/
int GetDataFromWebByte(char *szWebAddr,char *szMp3FileName , char *szRequest, char *szHeadEx, int iReqType, int iTimeout)
{
char szHost[] = "", szAddr[] = "";
char szWebBuf[] = "", szBuf[] = "";
char *pRetData = NULL; //返回的数据
char *szHead = NULL;
int iPort = , iAppSock;
int iRet = , iLen, k, iChars, iDone;
int iContentLen, iChunked = ;
struct sockaddr_in appHostAddr;
struct hostent *pHostent = NULL;
int iTransferType = , iContent = ;
FILE *fp;
WSADATA wsa={}; if(!szWebAddr)
return ;
WSAStartup(MAKEWORD(,),&wsa); //获取host和addr和port
iRet = GetHostandAddr(szWebAddr, szHost, sizeof(szHost), szAddr, sizeof(szAddr), &iPort);
if(iRet < )
return ; //获取hostent
pHostent = gethostbyname(szHost);
if(!pHostent)
{
iRet = WSAGetLastError(); return ;
} //创建socket
appHostAddr.sin_family = AF_INET;
appHostAddr.sin_port = htons((unsigned short)iPort);
appHostAddr.sin_addr.s_addr = ((struct in_addr *)(pHostent->h_addr))->s_addr;
iAppSock = socket(AF_INET, SOCK_STREAM, );
if(iAppSock == -)
{
return ;
} //将创建的Socket连接至应用服务器
if(connect(iAppSock, (void *)&appHostAddr, sizeof(appHostAddr)) == -)
{
return ;
} //配置httphead
szHead = GetHttpHead(szHost, szAddr, szRequest, szHeadEx, iReqType);
if(!szHead)
return ; //向应用服务器发送HTTP请求
if(send(iAppSock, szHead, strlen(szHead), ) == -){
return ;
}
free(szHead);
szHead = NULL; //接收应用服务器返回的数据
memset(szWebBuf, , sizeof(szWebBuf));
memset(szBuf, , sizeof(szBuf));
k = ;
iChars = ;
iDone = ;
//获取http头信息
while(iDone == )
{
iLen = RecvHttpData(iAppSock, szWebBuf, , iTimeout);
if(iLen < )
iDone = ;
switch(*szWebBuf)
{
case '\r':
break;
case '\n':
if(iChars == )
iDone = ;
iChars = ;
break;
default:
iChars++;
break;
}
if(k < sizeof(szBuf) - )
{
szBuf[k] = szWebBuf[];
k++;
}
else
{
pRetData = addRevData(pRetData, szBuf);
memset(szBuf, , sizeof(szBuf));
k = ;
szBuf[k] = szWebBuf[];
k++;
}
}
pRetData = addRevData(pRetData, szBuf); //获取http内容长度
//判断是否chunked
if(strstr(pRetData, "chunked"))
iChunked = ; //为chunk传输 //获取http内容
iContent = ;
do
{
if (iChunked > )
{
iContentLen = GetChunkedContentLength(iAppSock);
}
else
{
iContentLen = GetContentLength(pRetData);
}
//获取http内容
//清空数据
if(iContent == && pRetData)
{
free(pRetData);
pRetData = NULL;
iContent = ;
}
if(iContentLen < )
{
closesocket(iAppSock);
//close(iAppSock);
return ;
} //chunk
memset(szWebBuf, , sizeof(szWebBuf));
memset(szBuf, , sizeof(szBuf));
k = ;
iChars = iContentLen;
//*iLenByte = iChars;
iDone = ;
fp = fopen(szMp3FileName , "wb+");
while(iDone == )
{
iLen = RecvHttpData(iAppSock, szWebBuf, , iTimeout);
if(iLen < )
iDone = ;
iChars--;
if(iChars < )
iDone = ;
if(k < sizeof(szBuf) - )
{
szBuf[k] = szWebBuf[];
k++;
}
else
{
// puts(szBuf);
fwrite(szBuf,,,fp);
memset(szBuf, , sizeof(szBuf));
k = ;
szBuf[k] = szWebBuf[];
k++;
}
}
fclose(fp); } while (iChunked); //chunk格式才继续找 closesocket(iAppSock);
WSACleanup();
return ;
}
块的大小结合实际情况,却大越好,太小的时候,噪音的频率会很高,影响质量,具体消除噪音,以后再改进。