Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

http - Convert CURL to C#

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);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...