VC++ Post 方法 上传数据到web服务器
最近在做一个项目,需要与WEB服务器交互一些信息。其中一项就是文件的上传与下载。现来一个上传的代码
#include "stdio.h"
#include "WinSock2.h"
#include "iostream"
using namespace std;
#pragma comment(lib,"ws2_32.lib")
long l_file_len;
//获取文件内容
bool file_con(char **buffer,LPCSTR file)
{
FILE *fp = fopen(file, "rb");
if(fp==NULL)
{
printf("文件上传失败,请检查文件路径.....\n");
return false;
}
fseek(fp, , SEEK_END);
l_file_len = ftell(fp);
rewind(fp); *buffer = new char[l_file_len + ];
memset(*buffer, , l_file_len + );
fseek(fp, , SEEK_SET);
fread(*buffer, sizeof(char), l_file_len, fp);
fclose(fp);
return true;
} //文件上传
std::string upload(LPCSTR lpszServer,LPCSTR lpszAddr,LPCSTR fileUrl)
{
char *file = NULL;
if(!file_con(&file,fileUrl))
{
return "";
}
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
return "";
SOCKADDR_IN server;
server.sin_family = AF_INET;
server.sin_port = htons();
struct hostent *host_addr = gethostbyname(lpszServer);
if (host_addr == NULL)
return "host_addr == NULL";
server.sin_addr.s_addr = *((int *) *host_addr->h_addr_list);
if (::connect(sock, (SOCKADDR *) &server, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
::closesocket(sock);
return "";
}
printf("ip address = %s, port = %d\n",inet_ntoa(server.sin_addr), ntohs(server.sin_port)); std::string header("");
std::string content("");
//----------------------post头开始--------------------------------
header +="post ";
header +=lpszAddr;
header +=" HTTP/1.1\r\n";
header +="Host: ";
header +=lpszServer;
header += "\r\n";
header += "User-Agent: Mozilla/4.0\r\n";
header += "Connection: Keep-Alive\r\n";
header += "Accept: */*\r\n";
header += "Pragma: no-cache\r\n";
header += "Content-Type: multipart/form-data; charset=\"gb2312\"; boundary=----------------------------64b23e4066ed\r\n"; content += "------------------------------64b23e4066ed\r\n";
content += "Content-Disposition: form-data; name=\"file\"; filename=\"大论文和实验材料.rar\"\r\n";
content += "Content-Type: aapplication/octet-stream\r\n\r\n"; //post尾时间戳
std::string strContent("\r\n------------------------------64b23e4066ed\r\n");
char temp[] = {};
//注意下面这个参数Content-Length,这个参数值是:http请求头长度+请求尾长度+文件总长度
sprintf(temp, "Content-Length: %d\r\n\r\n", content.length()+l_file_len+strContent.length());
header += temp;
std::string str_http_request;
str_http_request.append(header).append(content);
//----------------------post头结束-----------------------------------
//发送post头
send(sock, str_http_request.c_str(), str_http_request.length(), ); char fBuff[];
int nPacketBufferSize = ; // 每个数据包存放文件的buffer大小
int nStart;//记录post初始位置
int nSize;//记录剩余文件大小
// 就分块传送
for (int i = ; i < l_file_len; i += nPacketBufferSize)
{
nStart=i;
if (i + nPacketBufferSize + > l_file_len)
{
nSize = l_file_len - i;
}
else
{
nSize = nPacketBufferSize;
} memcpy(fBuff, file + nStart, nSize);
::send(sock, fBuff, nSize, );
Sleep(0.2);
} ::send(sock,strContent.c_str(),strContent.length(),); char szBuffer[] = {};
while (true)
{ int nRet = ::recv(sock, szBuffer, sizeof(szBuffer), );
if (nRet == || nRet == WSAECONNRESET)
{
printf("Connection Closed.\n");
break;
}
else if (nRet == SOCKET_ERROR)
{
printf("socket error\n");
break;
}
else
{
printf("recv() returned %d bytes\n", nRet);
printf("received: %s\n", szBuffer);
break;
}
}
::closesocket(sock);
delete [] file;
return szBuffer;
}
void main()
{
WORD wVersionRequested=MAKEWORD(,);
WSADATA wsaData;
if(WSAStartup(wVersionRequested,&wsaData))
{
cout<<"加载错误"<<endl;
}
if(LOBYTE(wsaData.wVersion)!=||HIBYTE(wsaData.wHighVersion)!=)
{
cout<<"WinSock's 加载错误"<<endl;
}
upload("localhost","/WebApplication1/Default.aspx","F:\\postgresql-8.3.3-2.rar");
}
这是上传文件的C++代码
下面是用 asp.net写的服务端的接收数据的代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text; namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
try
{
HttpPostedFile file = Request.Files[0];
string filePath = Request.PhysicalApplicationPath + file.FileName;
file.SaveAs(filePath);
Response.Write("Success\r\n");
}
catch
{
Response.Write("Error\r\n");
}
Response.End();
}
}
}
} 上面就是简单的代码示例,web需配置iis,相信大家知道怎么做^_^,还有一点就是web程序默认上传文件的限制,默认上传大小为4M,所以需要
在web.config中设置以下参数:
[html] view plaincopy
<httpRuntime maxRequestLength="951200" appRequestQueueLimit="60" executionTimeout="60"/>
VC++ Post 方法 上传数据到web服务器的更多相关文章
- Java客户端通过Http发送POST请求上传文件到web服务器
http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html 1.朋友的一个需求,让我给他实现,需求是这样的,需要用ASP.n ...
- [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现)
一.安装XAMPP http://www.cnblogs.com/lidongxu/p/5256330.html 二. 配置MySql http://www.cnblogs.com/lidongx ...
- libcurl 上传文件至 web服务器
测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php) libcurl vc6 工程代码 下载地址: http://download.csdn.ne ...
- 微信的API都是通过https调用实现的,分为post方法调用和get方法调用。不需要上传数据的采用get方法(使用IntraWeb开发)
首先需要明确的是,微信的API都是通过https调用实现的,分为post方法调用和get方法调用.不需要上传数据的采用get方法(例如获取AccessToken),而需要向微信服务器提交数据的采用po ...
- 从app上传图片到php,再上传到java后端服务器的方法一览
在现在的网络开发中,上传图片类的需求实在是太普通不过了,但是对于怎么样做到上传图片,对于刚开始建立项目的时候,还是有点不知所措的.也许有幸,我们做的项目是之前已经有人写过类似的用例了,那么我们只需要依 ...
- ueditor富文本上传图片的时候报错"未找上传数据"
最近因为需求所以在ssh项目中使用了Ueditor富文本插件,但是在上传图片的时候总是提示“未找到上传数据”,之后百度了好久终于弄明白了.因为Ueditor在上传图片的时候会访问controller. ...
- 从app上传图片到php,再上传到java后端服务器的方法一条龙服务
在现在的网络开发中,上传图片类的需求实在是太普通不过了,但是对于怎么样做到上传图片,对于刚开始建立项目的时候,还是有点不知所措的.也许有幸,我们做的项目是之前已经有人写过类似的用例了,那么我们只需要依 ...
- 通过 微软 pai-fs 上传数据到HDFS (Microsoft OpenPAI)
准备环境 (个人使用记录,方便下次使用查阅~~) 首先保证PAI是登陆状态: 进入GitHub项目所在地址: https://github.com/Microsoft/pai/ 然后切换分支到 具体 ...
- LoadRunner 多用户并发 登录,上传数据,登出的脚本教程
这里记录 Web/Http 模式,模拟多用户并发进行 : 登录,上传数据,退出登录一整套流程.并发的用户量多少,可自定义.这里不介绍录屏的方式,是自己写脚本去执行的. 1.安装loadRunner ...
随机推荐
- HBase参数配置及说明
版本:0.94-cdh4.2.1 hbase-site.xml配置 hbase.tmp.dir 本地文件系统tmp目录,一般配置成local模式的设置一下,但是最好还是需要设置一下,因为很多文件都会默 ...
- C++调用JAVA方法详解
C++调用JAVA方法详解 博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...
- CentOS下安装Tomcat7
1.检查java版本信息 #java -version java version "1.7.0_65" OpenJDK Runtime Environment (rhel-2.5. ...
- 【HOW】如何允许编辑用户配置文件属性
在自定义用户配置文件属性后会发现,通过属性本身的配置页面,只能允许用户修改自己的属性,而管理员无法修改其他用户的属性.若要允许管理员在用户信息页面修改用户配置文件属性,可通过如下操作实现: 1. 进入 ...
- ios 逆向收录
lldb 远程调试环境搭建:http://www.cnblogs.com/csutanyu/p/3653252.html
- 华硕Z97-A主板声卡设置
$ vim /usr/share/alsa/alsa.conf ## defaults# # show all name hints also for definitions without hint ...
- Google App Engine, Python2.7的UnicodeDecodeError bug
在跟Web Development,要在Google App Engine上写作业,出师不利,遇到以下bug: 2014-05-06 16:14:17 Running command: "[ ...
- find命令和tar命令的使用
tar命令 tar -zcvf small.tar.gz small(目录名) (压缩) tar -zxvf small.tar.gz -C small(目录名) (解压到指定目录) find 命令 ...
- Oracle去掉字符串首尾
今天刚注册博客,与大家分享一下今天的新的: 今天在报表中碰到这样一个需求,数据库里面的一个字段是其他的3个字段合成的,但是现在读取数据只要中间的那一部分, 思考了许久这个字段的中间部分不是固定的,头和 ...
- 【整理】动态加载Web Services
WebClient client = new WebClient(); String url = "http://localhost/MESAPIWebService/MESAPI.asmx ...