該方案是將文件從server1(本地服務器)中的PHP應用程序發送到server2中的C#.net應用程序。無法使用cURL發佈文件
使用簡單的HTML表單發送文件時,目標服務器收到它沒有問題。
使用cURL發送該文件時,我從目標服務器收到錯誤500 請注意,我無法訪問目標服務器,並且其管理員確信一切正常,因爲HTML表單已成功提交。
這裏是我的代碼後的文件:
$filedata = $fileName; // Absolute path of the file ==> 'D:\wamp\www\my_project\upload\image\201401070657414.jpg'
$headers = array("Content-Type:multipart/form-data");
$postfields = array("filedata" => "@$filedata", "filename" => $fileName);
$ch = curl_init();
$options = array(
CURLOPT_URL => 'http://server2/postphoto.aspx', //500
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => filesize($filedata),
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
而且curl_getinfo($ CH)返回:
array (size=26)
'url' => string 'http://server2/postphoto.aspx' (length=41)
'content_type' => string 'text/html; charset=utf-8' (length=24)
'http_code' => int 500
'header_size' => int 265
'request_size' => int 203
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 0.032
'namelookup_time' => float 0
'connect_time' => float 0
'pretransfer_time' => float 0
'size_upload' => float 28998
'size_download' => float 7127
'speed_download' => float 222718
'speed_upload' => float 906187
'download_content_length' => float 7127
'upload_content_length' => float 28998
'starttransfer_time' => float 0.016
'redirect_time' => float 0
'certinfo' =>
array (size=0)
empty
...
什麼是錯我的代碼?
變量$迴應表示:
string 'HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Wed, 22 Jan 2014 08:31:54 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 6
nofile' (length=249)
Content-Type的rturns text/html的同時我已經把它在我的頭爲multipart/form-數據。
以下是在server2的Asp.net代碼:
if (Request.Files.Count == 0)
{
Response.Write("nofile");
return;
}
System.Web.HttpPostedFile file = Request.Files[0];
System.IO.Stream streamObj = file.InputStream;
Byte[] buffer = new Byte[file.ContentLength];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
System.IO.File.WriteAllBytes("d:\\photo\\" + file.FileName, buffer);
你應該發佈'c#'代碼。 –
我也用php測試過,得到了同樣的錯誤。 –
向我們展示「simple html form」的HTML源代碼,它與瀏覽器 –