我想将cURL脚本转换为C#脚本。 如果我想发布图像,我必须将其转换为字符串?cURL到C#webrequest
当我尝试运行我的脚本时,我从目标机器收到异常。不幸的是,我无法查看目标机器上的代码。
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:"));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC) ;
curl_setopt($ch, CURLOPT_USERPWD, "user:pwd");
curl_setopt($ch, CURLOPT_URL, 'http://192.168.1.105/upload.php');
$data = array('type' => 'upload', 'student' => 'Peter', 'class' => '101', 'fotograf' => 'Jane', 'file' => '@/pictures/image1.jpg');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
这是我想出了:
WebRequest request = WebRequest.Create("http://192.168.1.105/upload.php");
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
string authInfo = "usr:pwd";
request.Headers["Authorization"] = "Basic " + authInfo;
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("type=upload&student=Peter&calss=101&fotograf=Jane&file=/pictures/image1.jpg");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
WebResponse response = request.GetResponse();
任何想法?
您能否提供您所得到的确切异常?它应该有助于找到确切的问题。 – 2012-02-23 21:01:36
我相信因为你的C#代码没有进行图像上传到PhP代码中。在C#中没有相当于@的PHP,你必须在头文件中写边界,或者使用一些方法,例如从WebClient类中的[UploadFile()] [1]。 [1]:http://msdn.microsoft.com/en-us/library/36s52zhs.aspx – 2012-02-23 21:10:43
我得到的Exse是:(417)异常失败 – 2012-02-24 07:53:02