有懂cURL的高手在么?看看这一段怎么用C#实现

stonepeter 2011-01-01 06:14:07
有如下一段curl PHP代码,如今要在C#下实现,本人不懂PHP也不懂CURL,听说可以用C#代码实现.有高人可以帮忙么?

protected fuction curl_post_multi($url, $values) {
if (!is_array($values)) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, true);

$boundary = uniqid('---------------');
$MPboundary = '--'.$boundary;
$endMPboundary = $MPboundary. '--';
$multipartbody = '';
foreach ($values as $k => $v) {
if ($k == 'pic' || $k == 'image') {
$filename = 'pic.jpg';
$mime = 'image/jpeg';
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'Content-Disposition: form-data; name="' . $k . '"; filename . '"'. "\r\n";
$multipartbody .= 'Content-Type: '. $mime . "\r\n\r\n";
$multipartbody .= $v . "\r\n";
}
else {
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'content-disposition: form-data; name="' . $k . "\"\r\n\r\n";
$multipartbody .= $v . "\r\n";
}
}
$multipartbody .= $endMPboundary."\r\n";
}

下面这段代码调用楼上的
if( $method == "GET") {
$url_full = $url . '?' . http_build_query($params);
if ($debug) {
return $url_full;
}
return $this->curl_get($url_full);
}
else if ($parms['pic'] || $params['image'] || $params['content'])
return $this->curl_post_multi($url, $params);
}
else {
...
}
...全文
99 点赞 收藏 4
写回复
4 条回复
stonepeter 2011年01月01日
提交multipart/form-data表单数据的.
更完整的代码在这里
protected function curl_post_multi($url, $values) {
if (!is_array($values)) {
return false;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, true);

$boundary = uniqid('------------------');
$MPboundary = '--'.$boundary;
$endMPboundary = $MPboundary. '--';
$multipartbody = '';
foreach ($values as $k => $v) {
if ($k == 'pic' || $k == 'image') {
$filename = 'pic.jpg';
$mime = 'image/jpeg';
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'Content-Disposition: form-data; name="' . $k . '"; filename="' . $filename . '"'. "\r\n";
$multipartbody .= 'Content-Type: '. $mime . "\r\n\r\n";
$multipartbody .= $v . "\r\n";
}
else {
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'content-disposition: form-data; name="' . $k . "\"\r\n\r\n";
$multipartbody .= $v . "\r\n";
}
}
$multipartbody .= $endMPboundary."\r\n";
//echo $multipartbody;
curl_setopt($ch, CURLOPT_POSTFIELDS, $multipartbody);
$header_array = array("Content-Type: multipart/form-data; boundary=" . $boundary , "Expect: ");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);

$html = curl_exec($ch);
curl_close($ch);
return $html;
}
回复 点赞
wuyq11 2011年01月01日
主要是实现什么功能
回复 点赞
thisiscandy 2011年01月01日
不懂帮顶
回复 点赞
发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告