web服务器 接收文件,通过HTTPWebRequest发送文件并从$ _FILES在Web服务器上接收

我的.NET应用程序通过HttpWebRequest将单个简单文本文件发送到Web服务器 . 但是在Web服务器端,我总是得到一个空的$ _FILES数组 .

我读了所有这些问题和文章:

这是测试代码:

public static void UploadFile()

{

var boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

var httpRequest = (HttpWebRequest)WebRequest.Create(@"https://test.com/upload.php");

httpRequest.Credentials = CredentialCache.DefaultNetworkCredentials;

httpRequest.AllowAutoRedirect = true;

httpRequest.MaximumAutomaticRedirections = 1;

httpRequest.Method = "POST";

httpRequest.ContentType = "multipart/form-data; boundary=" + boundary;

httpRequest.KeepAlive = true;

using (var requestStream = httpRequest.GetRequestStream())

{

var data = Encoding.UTF8.GetBytes("--" + boundary + "\r\n\r\n");

requestStream.Write(data, 0, data.Length);

data = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n\r\n");

requestStream.Write(data, 0, data.Length);

data = Encoding.UTF8.GetBytes("1048576"); // 1Mb

requestStream.Write(data, 0, data.Length);

data = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");

requestStream.Write(data, 0, data.Length);

data = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"upload.txt\"\r\n");

requestStream.Write(data, 0, data.Length);

data = Encoding.UTF8.GetBytes("Content-Type: application/octet-stream\r\n\r\n");

requestStream.Write(data, 0, data.Length);

data = File.ReadAllBytes(@"D:\upload.txt");

requestStream.Write(data, 0, data.Length);

data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--");

requestStream.Write(data, 0, data.Length);

}

using (var response = (HttpWebResponse)httpRequest.GetResponse())

using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))

Console.WriteLine(sr.ReadToEnd());

}

/upload.php:

exit (var_dump($_FILES));

在控制台中,输出始终为: array(0){}

请帮忙 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值