- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 C++ 程序试图使用在 Apache2 网络服务器上运行的 PHP 应用程序上传文件。我有一个非常奇怪的问题,我不知道该怎么做。因此,我使用 httpSendRequest() 函数创建了 HTTP 请求,在 wireshark 中查看时它的格式似乎正确.然而,文件没有上传,当我查看 apache 的 access.log 文件时,它没有显示用户代理或内容长度,尽管两者都明显存在于数据包捕获中。
作为引用,返回 200 状态的请求与使用 burp 套件发送的数据包相同,而不是我使用 httpSendRequest 的程序发送的数据包,而且我也能够使用网络浏览器成功上传文件。
这是使用 httpSendRequest 创建请求的代码,其中大部分直接取自 this codeguru post .
#include <windows.h>
#include <wininet.h>
#include <iostream>
#define ERROR_OPEN_FILE 10
#define ERROR_MEMORY 11
#define ERROR_SIZE 12
#define ERROR_INTERNET_OPEN 13
#define ERROR_INTERNET_CONN 14
#define ERROR_INTERNET_REQ 15
#define ERROR_INTERNET_SEND 16
using namespace std;
int main()
{
// Local variables
static char *filename = "test.txt"; //Filename to be loaded
static char *type = "image/jpg";
static char boundary[] = "PaulRules"; //Header boundary
static char nameForm[] = "fileToUpload"; //Input form name
static char iaddr[] = "192.168.0.105"; //IP address
static char url[] = "upload.php"; //URL
char hdrs[255]; //Headers
char * buffer; //Buffer containing file + headers
char * content; //Buffer containing file
FILE * pFile; //File pointer
long lSize; //File size
size_t result;
// Open file
pFile = fopen ( filename , "rb" );
if (pFile==NULL) return ERROR_OPEN_FILE;
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
content = (char*) malloc (sizeof(char)*lSize);
if (content == NULL) return ERROR_MEMORY;
// copy the file into the buffer:
result = fread (content,1,lSize,pFile);
if (result != lSize) return ERROR_SIZE;
// terminate
fclose (pFile);
//allocate memory to contain the whole file + HEADER
buffer = (char*) malloc (sizeof(char)*lSize + 2048);
//print header
sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
sprintf(buffer,"--%s\r\nContent-Disposition: form-data; name=\"fileToUpload\"; filename=\"test.bmp\"\r\n",boundary,nameForm,filename);
sprintf(buffer,"%sContent-Type: %s\r\n\r\n",buffer,type);
int cb = strlen(buffer);
char * bp = buffer + cb;
memcpy(bp, content, lSize);
bp += lSize;
int cw = sprintf(bp,"\r\n--%s--\r\n",boundary);
//Open internet connection
HINTERNET hSession = InternetOpen("Paul was here :)",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession==NULL) return ERROR_INTERNET_OPEN;
HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(hConnect==NULL) return ERROR_INTERNET_CONN;
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",url, NULL, NULL, (const char**)"*/*", 0, 1);
if(hRequest==NULL) return ERROR_INTERNET_REQ;
BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, cb + lSize + cw);
if(!sent) return ERROR_INTERNET_SEND;
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 0;
}
这是服务器端的 PHP 脚本
<?php
$target_dir = "recvFile/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "File already exists";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "File to large";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "ERROR File not uploaded";
}
//attempt to upload the file
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
echo "Command:1";
}
else {
echo $_FILES["fileToUpload"]["tmp_name"];
echo "ERROR uploading file";
}
}
?>
最佳答案
您的源文件是 "test.txt"
但您正在上传到 "test.bmp"
。使它们具有相同的扩展名。
type = "image/jpg";
将 type
更改为 "image/*"
或 "text/*"
如果这应该是文本。
看到它打印出 "Command 1"
,您的操作可能是成功的。该文件可能在那里,但它不是您期望的那个。如果没有,请查看您可以从 PHP 端提取什么错误。
sprintf(buffer,"--%s\r\nContent-Disposition: form-data; \
name=\"fileToUpload\"; filename=\"test.bmp\"\r\n",
boundary,nameForm,filename);
您有一个格式说明符 "%s"
和 4 个参数。删除最后两个参数。
使用HttpOpenRequest
推荐方式:
const char *accept[] = { "image/*", NULL }; // or `"text/*"`
HttpOpenRequest(hConnect, "POST",url, NULL, NULL, accept, 0, 1);
#include <Windows.h>
#include <Wininet.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#pragma comment(lib, "wininet.lib")
int main()
{
HINTERNET hsession = NULL;
HINTERNET hconnect = NULL;
HINTERNET hrequest = NULL;
const char* server = "localhost";
const char* url = "upload.php";
const char* type = "image/*";
std::string filename = "test.bmp";
std::ifstream infile("c:\\test\\test.bmp", std::ios::binary);
if(!infile)
return 0;
std::ostringstream oss;
oss << infile.rdbuf();
std::string headers = "Content-type: multipart/form-data, boundary=uniquestring";
std::string data = "--uniquestring\r\n\
Content-Disposition: form-data; name=\"fileToUpload\"; filename=\"%1\"\r\n\
Content-Type: %2\r\n\
\r\n\
%3\r\n\
--uniquestring--";
data.replace(data.find("%1"), 2, filename);
data.replace(data.find("%2"), 2, type);
data.replace(data.find("%3"), 2, oss.str());
hsession = InternetOpen("appname", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(!hsession)
goto cleanup;
hconnect = InternetConnect(hsession, server, INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(!hconnect)
goto cleanup;
const char *accept[] = { type, NULL };
hrequest = HttpOpenRequest(hconnect, "POST", url, NULL, NULL, accept, 0, 1);
if(!hrequest)
goto cleanup;
BOOL sent = HttpSendRequest(hrequest, headers.data(), headers.size(),
&data[0], data.size());
if(sent)
{
DWORD bufsize = 4096;
std::string read(bufsize, 0);
InternetReadFile(hrequest, &read[0], bufsize, &bufsize);
read.resize(bufsize);
std::cout << read << "\n";
}
else
{
goto cleanup;
}
cleanup:
if(hrequest) InternetCloseHandle(hrequest);
if(hconnect) InternetCloseHandle(hconnect);
if(hsession) InternetCloseHandle(hsession);
return 0;
}
关于php - Apache2 access.log 对于相同的数据包不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47242628/
使用 Python 的 rtmplib 绑定(bind)并遇到一些问题。 首先, 我有这样的东西: import librtmp conn = librtmp.RTMP(...) conn.con
基本上,我是在查看 Motorstorm 排行榜时在 PS3 上窃听数据包。排行榜以 XML 格式发送到我的 ps3,但只有在我获得授权后。那么有人可以告诉我这三个数据包之间发生了什么,以及我如何在浏
我正在努力了解 TCP,但解析大量 RFC 并没有帮助。我相信我了解连接和关闭握手,但我似乎无法找到任何总结实际数据流的内容。 在连接和关闭握手之间 TCP 数据包看起来像什么? (特别是标题) 最佳
我正在尝试通过 RCON 端口与我的 Minecraft 服务器通信。 虽然我不知道如何使用套接字和流的东西。四处寻找,我发现他们都有一些共同点。套接字、输入流和输出流。 我在我的代码中试过了,但返回
我正在 UDP 之上设计一个简单的协议(protocol),现在我意识到其他人可以将数据包发送到我正在监听的端口。这样的数据包对于我的应用程序来说显然是不正确的(我现在不担心安全问题) 是否有过滤这些
我目前有一个具有可自定义滴答率的游戏服务器,但在本示例中,我们建议服务器每秒仅滴答一次或 1hz。我想知道如果客户端发送速率比服务器快,因为我当前的设置似乎不起作用,那么处理传入数据包的最佳方法是什么
我无法理解网络字节顺序以及通过 UDP 发送和接收数据的顺序。我正在使用 C#。 我有一个结构保持: message.start_id = 0x7777CCCC; message.me
我正在为 USB 设备编写代码。假设 USB 主机开始控制读取传输以从设备读取一些数据,并且请求的数据量(设置数据包中的 wLength)是端点 0 最大数据包大小的倍数。那么在主机接收到所有数据后(
我有一台 Windows PC、Marvell 交换机、Netgear 交换机和一台 Ubuntu 机器连接在一起(通过 Netgear 交换机)。 我最近从 Windows PC 向 Marvell
在查看数据包字节码时,您将如何识别 dns 数据包。 IP header 的协议(protocol)字段会告诉后面有一个 UDP 帧,但是在 UDP 帧内没有协议(protocol)字段来指定接下来会
我有一个通过 udf 的 802.11 (wifi) 上各种类型的流量的 pcap。由于 MTU,udp(或更准确地说是 IP)对 wifi 数据包进行分段。我目前正在使用 SharpPcap 读取并
我正在开发的 Core Audio 应用程序上有此崩溃日志。我目前正在调试它,所以我的问题不是关于崩溃本身,而是关于 的含义“k”包 . 这是什么意思 ? 我已阅读 this , 和 this (关于
我在一台 VM Ubuntu 16.04 机器上的 100 个多播组上生成 UDP 数据包,并在另一台 VM Ubuntu 16.04 机器上订阅这些组。两者都在由 Hyper-V 管理器运行的 HP
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (66 个回答) 6年前关闭。 我正在尝试创建一个简单的
我正在寻找使用 Java 来欺骗 UDP 数据包。是否有任何好的 Java 库可以让您创建自己的 RAW SOCKETS? 最佳答案 我会使用包装 libpcap 的 Java API . libpc
我在基于 Tyrus 的客户端和 tomcat Web 服务器之间使用没有压缩的 websocket。我在 tomcat 端看到消息传入和传出我的套接字,但如果我设置一个wireshark来观察它们传
我的应用程序在模拟器中运行时无法接收 UDP 数据包。 UDP 数据包由“localhost”上的以下 java 程序通过端口 49999 发送。 DatagramSocket clien
我正在开发一个 Google Glass 应用程序,它需要在工作线程中监听 UDP 数据包(与发送 UDP 数据包的现有系统集成)。我之前发布了一个问题(请参阅 here )并收到了一个答案,其中提供
我正在从客户端向服务器发送两个数据包。我遇到的问题是,在服务器上读取的数据使两个字符串对于发送的最长字符串具有相同的长度。例如: 如果字符串 1 为:1234 字符串 2 为:abcdefghi 服务
我知道这是不好的做法,但是可以执行以下操作吗? Send packet1 to UDP port 1 port 1 receives packet1 and sends it to port 2 po
我是一名优秀的程序员,十分优秀!