HTTP请求post数据

yule253456724 2011-11-29 10:45:19

char file[4096];
sprintf(request, "POST %s HTTP/1.1\r\nAccept:*/*\r\nAccept-Language:zh-cn\r\nHost:%s:%d\r\nContent-Type:application/x-www-form-urlencoded\r\nContent-Length:%d\r\nConnection:Close\r\n\r\nfile=%s\r\n\r\n", url ,host_addr, portnumber,len_subject,file);

file参数的值是从文件中读取的数据
但是文件是大于4K的
怎么post呢?
...全文
702 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2011-11-29
  • 打赏
  • 举报
回复
那你就发啊,为什么非要格式化到一个字符串里,C语言只有字节流,没有字符串什么的,这不是什么神奇的东西啊.
vilnies 2011-11-29
  • 打赏
  • 举报
回复
不一定要一次完成啊,分多次发送
qq120848369 2011-11-29
  • 打赏
  • 举报
回复
大于4K会发生什么.
yule253456724 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 vilnies 的回复:]
看你打的代码各种悲剧呀。。半路出家的吗
[/Quote]
- -!
vilnies 2011-11-29
  • 打赏
  • 举报
回复
看你打的代码各种悲剧呀。。半路出家的吗
crakme 2011-11-29
  • 打赏
  • 举报
回复
基于http的没弄过,基于TCP/IP的倒是会
yule253456724 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 a615625635 的回复:]
与JSP的 不同啊 帮不了
[/Quote]
jsp只是个测试用的页面
  • 打赏
  • 举报
回复
与JSP的 不同啊 帮不了
yule253456724 2011-11-29
  • 打赏
  • 举报
回复
测试代码jsp

<%@page contentType="text/html; charset=GBK"%>
<%@ page import="java.util.*" %>
<%

System.out.println("Visitor parameter dump:");
System.out.println("Protocol: " + request.getProtocol() + "<br>");
System.out.println("Scheme: " + request.getScheme() + "<br>");
System.out.println("Server Name: " + request.getServerName() + "<br>" );
System.out.println("Server Port: " + request.getServerPort() + "<br>");

System.out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo() + "<br>");
System.out.println("Remote Addr: " + request.getRemoteAddr() + "<br>");
System.out.println("Remote Host: " + request.getRemoteHost() + "<br>");
System.out.println("Character Encoding: " + request.getCharacterEncoding() + "<br>");
System.out.println("Content Length: " + request.getContentLength() + "<br>");
System.out.println("Content Type: "+ request.getContentType() + "<br>");
System.out.println("Auth Type: " + request.getAuthType() + "<br>");
System.out.println("HTTP Method: " + request.getMethod() + "<br>");
System.out.println("Path Info: " + request.getPathInfo() + "<br>");
System.out.println("Path Trans: " + request.getPathTranslated() + "<br>");
System.out.println("Query String: " + request.getQueryString() + "<br>");
System.out.println("Remote User: " + request.getRemoteUser() + "<br>");
System.out.println("Session Id: " + request.getRequestedSessionId() + "<br>");
System.out.println("Request URI: " + request.getRequestURI() + "<br>");
System.out.println("Servlet Path: " + request.getServletPath() + "<br>");
System.out.println("Accept: " + request.getHeader("Accept") + "<br>");
System.out.println("Host: " + request.getHeader("Host") + "<br>");
System.out.println("Referer : " + request.getHeader("Referer") + "<br>");
System.out.println("Accept-Language : " + request.getHeader("Accept-Language") + "<br>");
System.out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding") + "<br>");
System.out.println("User-Agent : " + request.getHeader("User-Agent") + "<br>");
System.out.println("Connection : " + request.getHeader("Connection") + "<br>");
System.out.println("Cookie : " + request.getHeader("Cookie") + "<br>");
System.out.println("Created : " + session.getCreationTime() + "<br>");
System.out.println("LastAccessed : " + session.getLastAccessedTime() + "<br>");

%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>综合查询</title>
</head>
<body marginwidth=0 marginheight=0 topmargin=10 leftmargin=10 bgcolor="#9ACDE9" >
<%
out.println("Protocol: " + request.getProtocol() + "<br>");
out.println("Scheme: " + request.getScheme() + "<br>");
out.println("Server Name: " + request.getServerName() + "<br>" );
out.println("Server Port: " + request.getServerPort() + "<br>");

out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo() + "<br>");
out.println("Remote Addr: " + request.getRemoteAddr() + "<br>");
out.println("Remote Host: " + request.getRemoteHost() + "<br>");
out.println("Character Encoding: " + request.getCharacterEncoding() + "<br>");
out.println("Content Length: " + request.getContentLength() + "<br>");
out.println("Content Type: "+ request.getContentType() + "<br>");
out.println("Auth Type: " + request.getAuthType() + "<br>");
out.println("HTTP Method: " + request.getMethod() + "<br>");
out.println("Path Info: " + request.getPathInfo() + "<br>");
out.println("Path Trans: " + request.getPathTranslated() + "<br>");
out.println("Query String: " + request.getQueryString() + "<br>");
out.println("Remote User: " + request.getRemoteUser() + "<br>");
out.println("Session Id: " + request.getRequestedSessionId() + "<br>");
out.println("Request URI: " + request.getRequestURI() + "<br>");
out.println("Servlet Path: " + request.getServletPath() + "<br>");
out.println("Accept: " + request.getHeader("Accept") + "<br>");
out.println("Host: " + request.getHeader("Host") + "<br>");
out.println("Referer : " + request.getHeader("Referer") + "<br>");
out.println("Accept-Language : " + request.getHeader("Accept-Language") + "<br>");
out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding") + "<br>");
out.println("User-Agent : " + request.getHeader("User-Agent") + "<br>");
out.println("Connection : " + request.getHeader("Connection") + "<br>");
out.println("Cookie : " + request.getHeader("Cookie") + "<br>");
out.println("Created : " + session.getCreationTime() + "<br>");
out.println("LastAccessed : " + session.getLastAccessedTime() + "<br>");
out.println("parameter dump : <br>");
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
System.out.print( valueStr + values[i] + ",");
out.println(name+"["+i+"]="+values[i]+"<br>");
}
System.out.print("\n");
}
System.out.println("Visitor parameter end.");
%>

</body>
</html>



yule253456724 2011-11-29
  • 打赏
  • 举报
回复
代码贴出 求指正
发送文件读取不出来 发送 object 和 subject 都可以

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <limits.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <memory.h>

/********************************************
功能:把字符串转换为全小写
********************************************/
void ToLowerCase(char * s) {
while(s && *s) {*s=tolower(*s);s++;}
}

/**************************************************************
功能:从字符串src中分析出网站地址
***************************************************************/
void GetHost(char * src, char * web) {
char * pA;
char * pB;
memset(web, 0, sizeof(web));
if(!(*src)) return;
pA = src;
if(!strncmp(pA, "http://", strlen("http://"))) pA = src+strlen("http://");
else if(!strncmp(pA, "https://", strlen("https://"))) pA = src+strlen("https://");
pB = strchr(pA, '/');
if(pB) {
memcpy(web, pA, strlen(pA) - strlen(pB));
}
else memcpy(web, pA, strlen(pA));
if(pB) web[strlen(pA) - strlen(pB)] = 0;
else web[strlen(pA)] = 0;
pA = strchr(web, ':');
}


int main (int argc, char *argv[])
{
int len=0;
int lengh=0; //数据长度
int count=0; //读取文件次数
int sockfd;
char buffer[4096]; //接收数据
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber=80;
int nbytes; //发送请求的长度
char host_addr[256];
char * url;
char request[5120]; //发送请求
int send, totalsend;
int i;
char * pt;
char * object=""; // 对象
char * subject=""; // 主题
char * file="";
char * object_="object=";
char * subject_="subject=";
char * file_="file=";
unsigned char buffer_read[4096]; //从文件中读取的详情
int nRead;

int oc;
int opterr=0;
char * host_ip;
while((oc = getopt(argc, argv, "h:u:p:o:s:f:")) != -1)
{
switch(oc)
{
case 'h':
host_ip=optarg;
break;
case 'u':
url=optarg;
break;
case 'p':
portnumber=atoi(optarg);
break;
case 'o':
object=optarg;
break;
case 's':
subject=optarg;
break;
case 'f':
file=optarg;
break;
case '?':
printf("arguments error!\n");
break;
}
}
printf("parameter.1 is: %s\n", host_ip);
ToLowerCase(host_ip);/*将参数转换为全小写*/
printf("lowercase parameter.1 is: %s\n", host_ip);

GetHost(host_ip, host_addr);/*分析网址*/
printf("webhost:%s\n", host_addr);
printf("portnumber:%d\n\n", portnumber);

if((host=gethostbyname(host_addr))==NULL)/*取得主机IP地址*/
{
fprintf(stderr,"Gethostname error, %s\n", strerror(errno));
exit(1);
}
printf("hostname:%s\n",host->h_name);
printf("aliase:%s\n",host->h_aliases);
/* 客户程序开始建立 sockfd描述符 */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)/*建立SOCKET连接*/
{
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}

/* 客户程序填充服务端的资料 */
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);

/* 客户程序发起连接请求 */
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*连接网站*/
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}


FILE *pFileS = fopen( file, "rb" );
if(!pFileS)
{
file_="";
}
else
{
while( nRead = fread( buffer_read, sizeof(unsigned char),(sizeof(buffer_read)-1), pFileS ) )
{
memset(buffer_read + nRead,0,sizeof(buffer_read) - nRead);
len=0;
for (len;len<sizeof(buffer_read);len++)
{
if (buffer_read[len]=='\0')
{
break;
}
}
count++;
}
}
if (subject[0]=='\0')
{
subject_="";
}
if (object[0]=='\0')
{
object_="";
}

lengh=2+(count-1)*(sizeof(buffer_read)-1)+len+strlen(file_)+strlen(object_)+strlen(subject_)+strlen(object)+strlen(subject);

sprintf(request, "POST %s HTTP/1.1\r\nAccept:*/*\r\nAccept-Language:zh-cn\r\nHost:%s:%d\r\nContent-Type:application/x-www-form-urlencoded\r\nContent-Length:%d\r\nConnection:Close\r\n\r\n%s%s&%s%s&", url ,host_addr, portnumber,lengh,object_,object,subject_,subject);
printf("%s", request);/*准备request,将要发送给主机*/
int byte=strlen(request);
send = 0;totalsend = 0;
/*发送请求头*/
send = write(sockfd, request, byte);
if(send==-1)
{
printf("send error!%s\n", strerror(errno));
exit(0);
}

/*发送http请求文件数据*/
if(pFileS)
{
printf("read file\n");
while( nRead = fread( buffer_read, sizeof(unsigned char),(sizeof(buffer_read)-1), pFileS ) )
{
printf("reading\n");
memset(buffer_read + nRead,0,sizeof(buffer_read) - nRead);
int len=0;
for (len;len<sizeof(buffer_read);len++)
{
if (buffer_read[len]=='\0')
{
break;
}
}
sprintf(request, "%s%s", file_,buffer_read);
printf( "%s%s", file_,buffer_read);
printf("%s", request);/*准备request,将要发送给主机*/
nbytes=strlen(request);
while(totalsend < nbytes)
{
send = write(sockfd, request + totalsend, nbytes - totalsend);
if(send==-1)
{
printf("send error!%s\n", strerror(errno));
exit(0);
}
totalsend+=send;
printf("%d bytes send OK!\n", totalsend);
}

}
}

sprintf(request, "\r\n\r\n");
send = write(sockfd, request , strlen(request) );
if(send==-1)
{
printf("send error!%s\n", strerror(errno));
exit(0);
}
i=0;
/* 连接成功了,接收http响应,response */
while((nbytes=read(sockfd,buffer,1))==1)
{
if(i < 4)
{
if(buffer[0] == '\r' || buffer[0] == '\n')
i++;
else
i = 0;
printf("%c", buffer[0]);/*把http头信息打印在屏幕上*/
}
buffer[i]='\0';
printf(buffer);
}
printf("\n");
/* 结束通讯 */
close(sockfd);
exit(0);
}

hahayezhe112 2011-11-29
  • 打赏
  • 举报
回复
弄个循环 recv了 哎呀 囧死了!
yule253456724 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qq120848369 的回复:]
大于4K会发生什么.
[/Quote]
请详细指点一下吧
刚接触
yule253456724 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 vilnies 的回复:]
不一定要一次完成啊,分多次发送
[/Quote]
对 就是这个意思 但是我不会
是write完POST头之后 循环write数据吗?
IVERS0N 2011-11-29
  • 打赏
  • 举报
回复
直接post

69,336

社区成员

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

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