卷曲到C#的WebRequest [英] cURL to C# webrequest

查看:173
本文介绍了卷曲到C#的WebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图卷曲脚本转换为C#脚本。
如果我想张贴图片我必须把它转换为字符串?

I am trying to convert a cURL script to a C# script. If I want to POST an image do I have to convert it to a string?

当我尝试运行我的剧本我从目标机器的异常。不幸的是我没有访问看到目标机器上的代码

When I try running my script I get an exception from the target machine. Unfortunately I don't have access to see the code on the target machine.

$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();



任何想法?

Any ideas?

推荐答案

可能存在的问题与Authorization头并替换:

Possibly there is problem with the Authorization header and replacing:

string authInfo = "usr:pwd";
request.Headers["Authorization"] = "Basic " + authInfo;



with:

request.UseDefaultCredentials = false;
request.Credentials = new NetworkCredential(user, pwd);

可能会解决这个问题。

这篇关于卷曲到C#的WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆