高分求助:一段PHP利用CURL POST的代码如何用C#实现呢,求高手指点下

「已注销」 2013-08-05 07:54:28
这是PHP的代码,运行无误:
$pvars = array(
// parameters required by OAuth
"oauth_consumer_key" => $API_key,
"oauth_signature_method" => $oauth_signature_method,
"oauth_signature" => $oauth_signature,
"oauth_timestamp" => $oauth_timestamp,
"oauth_nonce" => $oauth_nonce,
"oauth_token" => $oauth_token,
// optional parameters
"content_type" => "animal",
"thumb_format" => "JPG",
"thumb_size" => "100*100",
"thumb_cropping" => 0,
"thumb_info" => 0,
"gallery_id" => "jg3qh55ad9ww7x29mwqh4bjt395fbdkv",
"response_format" => "JSON",
"image" => "@/home/images/123123.jpg"
);
// initialize curl and set parameters
$curl = curl_init();
print_r($curl);
curl_setopt($curl, CURLOPT_URL, 'http://www.axlan.com/API/resource/upload_image');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// execute, get information and close connection
echo $response = curl_exec($curl);
echo $info = curl_getinfo($curl);
curl_close ($curl);
// decode the json-encoded response
$response_array = json_decode($response);

下边是我引用CURL类库,根据以上代码改造的:
// populate an array with parameters
var pvars = new Dictionary<string, string>
{
// parameters required by OAuth
{"oauth_consumer_key", API_key},
{"oauth_signature_method", oauth_signature_method},
{"oauth_signature", oauth_signature},
{"oauth_timestamp", oauth_timestamp},
{"oauth_nonce", oauth_nonce},
{"oauth_token", oauth_token},

// optional parameters
{"content_type", "family"},
{"thumb_format", "JPG"},
{"thumb_size", "100x100"},
{"thumb_cropping", "0"},
{"thumb_info" , "1"},
{"gallery_id", "jg3qh55ad9ww7x29mwqh4bjt395fbdkv"},
{"response_format" , "JSON"},
{"image" , @"D:\images\abcdef.jpg"}, // ATTENTION - note the @ infront of the filename!
};

Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
Easy easy = new Easy();
Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
easy.SetOpt(CURLoption.CURLOPT_URL, "http://www.axlan.com/API/resource/upload_image");
easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
easy.SetOpt(CURLoption.CURLOPT_TIMEOUT,30);
easy.SetOpt(CURLoption.CURLOPT_POST,1);
easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS,pvars);
easy.SetOpt(CURLoption.CURLOPT_TRANSFERTEXT,true);
easy.Perform();
easy.Cleanup();
Curl.GlobalCleanup();

这样就无法运行,关键是不报错,不知道如何解决,
我用c# HttpWebRequest写了下,也没有实现.求高手指点下
...全文
202 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jshi123 2013-08-06
  • 打赏
  • 举报
回复
用HttpWebRequest的话试试下面这样调用:

	public static string UploadFileAndValues(string url, string[] files, Dictionary<string, string> values)
	{
		var request = WebRequest.Create(url);
		request.Method = "POST";
		var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
		request.ContentType = "multipart/form-data; boundary=" + boundary;
		boundary = "--" + boundary;

		using (var requestStream = new StreamWriter(request.GetRequestStream(), Encoding.UTF8))
		{
			foreach (string name in values.Keys)
			{
				requestStream.WriteLine(boundary);
				requestStream.WriteLine("Content-Disposition: form-data; name=\"{0}\"\n", name);
				requestStream.WriteLine(HttpUtility.UrlEncode(values[name]));
			}
			foreach (var f in files)
			{
				var file = new FileInfo(f);
				requestStream.WriteLine(boundary);
				requestStream.WriteLine("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", file.Name, file.Name);
				requestStream.WriteLine("Content-Type: application/octet-stream\n");
				requestStream.Write(File.ReadAllBytes(file.FullName));
				requestStream.WriteLine();
			}
			requestStream.Write(boundary + "--");
		}

		using (var response = request.GetResponse())
		{
			return new StreamReader(response.GetResponseStream()).ReadToEnd();
		}
	}
调用方法是: UploadFileAndValues("http://www.axlan.com/API/resource/upload_image", new[] {@"D:\images\abcdef.jpg"}, pvars); // pvars中的image项可以不写了
jshi123 2013-08-06
  • 打赏
  • 举报
回复
试试在文件名前面加上@符号 {"image" , @"@D:\images\abcdef.jpg"},
「已注销」 2013-08-06
  • 打赏
  • 举报
回复
求助求助..求指点啊! 用httpclient如何实现呢,网站返回的数据显示没有上传图片,其他部分是没错的.如何把图片流一起传送呢?

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧