GET and POST for ASP.NET MVC via C #

I have a windows application written in C #. This application will be deployed to my custom desktop computers. It will interact with background content that has already been created. The content is written in ASP.NET MVC 3. It provides several GET and POST operations, as shown here:

[AcceptVerbs(HttpVerbs.Get)] 
public ActionResult GetItem(string id, string caller) 
{ 
  // Do stuff 
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveItem(string p1, string p2, string p3)
{
  // Do stuff
}

The web developers in my team successfully interact with these operations through jQuery. Therefore, I know that they work. But I need to figure out how to interact with them from my Windows C # application. I used WebClient but ran into some performance issues, so I was advised to use the WebRequest object. Honest effort to try to do this, I tried the following:

WebRequest request = HttpWebRequest.Create("http://www.myapp.com/actions/AddItem"); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded";  
request.BeginGetResponse(new AsyncCallback(AddItem_Completed), request); 

, , ( ) . GET POST? - ? !

+3
2

- .           .

        string requestXml = "someinputxml";
        byte[] bytes = Encoding.UTF8.GetBytes(requestXml);

        var request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentLength = bytes.Length;
        request.ContentType = "application/xml";

        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            statusCode = response.StatusCode;

            if (statusCode == HttpStatusCode.OK)
            {                   
                responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
        }
+4

, WebClient - :

NameValueCollection postData = new NameValueCollection();
postData["field-name-one"] = "value-one";
postData["field-name-two"] = "value-two";

WebClient client = new WebClient();
byte[] responsedata = webClient.UploadValues("http://example.com/postform", "POST", postData);

?

+1

All Articles