如何将curl请求转换为C#中的WebRequest?
我试图卷曲的请求转换为WebRequest
在C#中,但没能得到回应,因为它总是返回401未经授权错误如何将curl请求转换为C#中的WebRequest?
这里是工作卷曲要求:
curl -k -v https://api.demo.peertransfer.com/v1/transfers -H“Content-Type:application/json”-X POST -d“{\”provider \“:\”HUL \“,\”payment_destination \“:\”hult-applicationfee \“, \“amount \”:“29000”,“callback_url”:\“http://studentapplication.local/en/nextsteps \”,\“callback_id \”:\“abc1234546asas \”,\“dynamic_fields \”:{\“student_id \ “32453245 \”,\“ST udent_first_name \ “:\” 糖果\ “\ ”student_last_name \“:\ ”学生\“}}” -H “X-Peertransfer-文摘:zYUt + Pn0A06wsSbCrrbAZn68Aslq9CbSUAKBrUEwIzI =”
输出:
结果在红盒子是什么,我需要在WebRequest
反应得到, 这里是我的C#代码
private void testnewfunc()
{
string value = "{\"provider\":\"HUL\",\"payment_destination\":\"hult-applicationfee\",\"amount\":\"29000\",\"callback_url\":\"http://studentapplication.local/en/nextsteps\",\"callback_id\":\"abc1234546asas\",\"dynamic_fields\":{\"student_id\":\"32453245\",\"student_first_name\":\"Candy\",\"student_last_name\":\"Student\"}}";
var URI = new Uri("https://api.demo.peertransfer.com/v1/transfers");
byte[] data = System.Text.ASCIIEncoding.Default.GetBytes(value);
var requst = (HttpWebRequest)WebRequest.Create(URI);
requst.UserAgent = "curl/7.43.0";
requst.Method = "POST";
requst.KeepAlive = true;
requst.AllowAutoRedirect = true;
requst.ContentType = " application/json";
requst.ContentLength = data.Length;
// wc.Headers["Content-Type"] = "application/json";
requst.Accept = "*/*";
//Or any other encoding type.
string result = System.Convert.ToBase64String(data);
var uname = "hultdemo2015";
var pword = "gEejgC0GF8pCbI7C";
var creds = string.Format("{0}:{1}", uname, pword);
creds = Convert.ToBase64String(Encoding.ASCII.GetBytes(creds));
requst.Headers["Authorization"] = string.Format("{0} {1}", "Basic", creds);
requst.Headers["X-Peertransfer-Digest"] = string.Format("{0}", "zYUt+Pn0A06wsSbCrrbAZn68Aslq9CbSUAKBrUEwIzI=");
using (Stream stream = requst.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var httpResponse = (HttpWebResponse)requst.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
// return true;
}
}
不知道什么,我在作出错误。
这里是我的解决方案:
var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false });
var message = new HttpRequestMessage(HttpMethod.Post, new Uri("https://api.demo.peertransfer.com/v1/transfers"));
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
message.Headers.Add("X-Peertransfer-Digest", "zYUt+Pn0A06wsSbCrrbAZn68Aslq9CbSUAKBrUEwIzI=");
message.Content = new StringContent("{\"provider\":\"HUL\",\"payment_destination\":\"hult-applicationfee\",\"amount\":\"29000\",\"callback_url\":\"http://studentapplication.local/en/nextsteps\",\"callback_id\":\"abc1234546asas\",\"dynamic_fields\":{\"student_id\":\"32453245\",\"student_first_name\":\"Candy\",\"student_last_name\":\"Student\"}}", Encoding.UTF8, "application/json");
var responseMessage = await client.SendAsync(message);
MessageBox.Show(string.Format("Status Code: {0}{1}Content-Type: {2}{1}Date: {3}{1}Location:{4}", responseMessage.StatusCode, Environment.NewLine, responseMessage.Content.Headers.ContentType, responseMessage.Headers.Date, responseMessage.Headers.Location));
下面是来自服务器的响应(同卷曲):
版本与.NET 4.0
var data = "{\"provider\":\"HUL\",\"payment_destination\":\"hult-applicationfee\",\"amount\":\"29000\",\"callback_url\":\"http://studentapplication.local/en/nextsteps\",\"callback_id\":\"abc1234546asas\",\"dynamic_fields\":{\"student_id\":\"32453245\",\"student_first_name\":\"Candy\",\"student_last_name\":\"Student\"}}";
var request = (HttpWebRequest)WebRequest.Create(new Uri("https://api.demo.peertransfer.com/v1/transfers"));
request.Method = "POST";
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.Headers.Add("X-Peertransfer-Digest", "zYUt+Pn0A06wsSbCrrbAZn68Aslq9CbSUAKBrUEwIzI=");
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (var reqStream = request.GetRequestStream())
using (var writer = new StreamWriter(reqStream))
{
writer.Write(data);
}
var response = request.GetResponse();
MessageBox.Show(response.Headers.ToString());
兼容
确保你有这些using语句:
using System.IO;
using System.Net;
,这些都是来自服务器的响应头:
嘿,我有一个问题,我不能使用await/async,因为我的框架是4.0无论如何,我可以做到这一点没有这两个? – Ramesh
@Ramesh看我的编辑 – Nasreddine
非常感谢你...它的工作完美 – Ramesh
你在你的'curl'例如没有'Authorization'头。你如何获得授权? –
curl请求的发送没有授权标头。 @Cᴏʀʏ – Ramesh
但是API需要C#代码的证书吗?这似乎很奇怪。你的'WebRequest'中的所有东西看起来都不错,但我会确切地知道你需要'user-agent'和'authorization'头文件。这就是你的'WebRequest'与curl请求不同的地方。 –