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

问题描述

我已经花了年龄尝试各种不同的方法来此卷曲转换为C#。可能有人请帮助。
我试图做一个HTTP POST和不断收到错误500
在这里我要转换的内容:

 卷曲--user用户名:密码-X POST -D浏览器= Win7x64-C1 | Chrome32 | 1024×768&安培; URL = HTTP://www.google.comhttp://crossbrowsertesting.com/api/v3 / livetests / 

和这是我迄今为止:

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(baseURL时); 
request.Method =POST;
request.Accept =应用/ JSON;
request.Credentials =新的NetworkCredential(用户名,密码);

VAR响应= request.GetResponse();
字符串文本;

使用(VAR SR =新的StreamReader(response.GetResponseStream()))
{
文本= sr.ReadToEnd();
values.Add(文本);
}



试过这个方法太多,但它没有工作:

 列表<串GT;数据=新的List<串GT;(); 
data.Add(浏览器= Win7x64-C1 | Chrome20 | 1024×768);
data.Add(URL = URL);
data.Add(FORMAT = JSON);
data.Add(回调= DOIT);
VAR请求= WebRequest.Create(CrossBrowserTestingURL);
request.Method =POST;
request.ContentType =应用/的X WWW的形式,进行了urlencoded
request.Credentials =新的NetworkCredential(用户名,密码);
使用(VAR作家=新的StreamWriter(request.GetRequestStream()))
{
writer.Write(数据=+数据);
}

VAR响应= request.GetResponse();

字符串文本;

使用(VAR SR =新的StreamReader(response.GetResponseStream()))
{
文本= sr.ReadToEnd();
values.Add(文本);
}


解决方案

我修改了第一个数据写入请求流作为每,做这项工作:

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(baseURL时); 
request.Method =POST;
request.Accept =应用/ JSON;
request.Credentials =新的NetworkCredential(用户名,密码);
request.UserAgent =卷曲/ 7.37.0
request.ContentType =应用/的X WWW的形式,进行了urlencoded使用

(VAR的StreamWriter =新的StreamWriter(request.GetRequestStream()))
{
字符串数据=浏览器= Win7x64-C1 | Chrome32 | 1024×768&安培; URL = HTTP: //www.google.com;

streamWriter.Write(数据);
}

VAR响应= request.GetResponse();
字符串文本;

使用(VAR SR =新的StreamReader(response.GetResponseStream()))
{
文本= sr.ReadToEnd();
values.Add(文本);
}


I have spent ages trying various different ways to convert this curl to c#. Could someone please help.I am trying to do a http post and keep getting error 500.here is what I want to convert:

curl --user username:password -X POST -d "browser=Win7x64-C1|Chrome32|1024x768&url=http://www.google.com" http://crossbrowsertesting.com/api/v3/livetests/

and this is what I have so far:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseurl);
request.Method = "POST";
request.Accept = "application/json";
request.Credentials = new NetworkCredential(username, password);

var response = request.GetResponse();
string text;

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
    values.Add(text);
}

Tried this method too but it didn't work:

List<string> data = new List<string>();
        data.Add("browser=Win7x64-C1|Chrome20|1024x768");
        data.Add("url=URL");
        data.Add("format=json");
        data.Add("callback=doit");
        var request = WebRequest.Create("CrossBrowserTestingURL");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Credentials = new NetworkCredential(username, password);
        using (var writer = new StreamWriter(request.GetRequestStream()))
        {
            writer.Write("data=" + data);
        }

        var response = request.GetResponse();

        string text;

        using (var sr = new StreamReader(response.GetResponseStream()))
        {
            text = sr.ReadToEnd();
            values.Add(text);
        }
解决方案

I modified the first one to write data to the request stream as per http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx, does this work:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseurl);
request.Method = "POST";
request.Accept = "application/json";
request.Credentials = new NetworkCredential(username, password);
request.UserAgent = "curl/7.37.0";
request.ContentType = "application/x-www-form-urlencoded";

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    string data = "browser=Win7x64-C1|Chrome32|1024x768&url=http://www.google.com";

    streamWriter.Write(data);
}

var response = request.GetResponse();
string text;

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
    values.Add(text);
}

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

08-22 21:18