KRIZTE KRIZTE - 1 year ago 334
C# Question

WebClient - UploadValues : Get Status Response

I'am trying to pass values from a controller to another controller in another domain. I'am adding data to a NameValueCollection and pass it to another controller [httppost] method and receiving data there mapped to a Model same as i passed from.

Currently i'am running it locally by opening two instance of VS simultaneously. When the both VS is opened the values are passed correctly and the information is written to db correctly and i receive a response like "{byte[0]}". Now when i try stopping the destination controller Project and try to submit data then it wont work but still i get the same response as "{byte[0]}". Can somebody please help me how to return the response command in this scenario. Is there a way a understand the UploadValues are completed or not completed.


NameValueCollection resumeDetails = new NameValueCollection();
resumeDetails.Add("FirstName", "KRIZTE");

byte[] res = this.Post(ConfigurationManager.AppSettings["RedirectionUrl"].ToString(), resumeDetails);

return View("Index");

public byte[] Post(string uri, NameValueCollection resumeDetails)
byte[] response = null;
WebClient client = new WebClient();
response = client.UploadValues(uri, resumeDetails);
return response;

Answer Source

you should not use the WebClient because problems like this.

Microsoft implemented HttpClient class as a newer api and it has these benefits:

HttpClient is the newer of the APIs and it has the benefits of

has a good async programming model

1- being worked on by Henrik F Nielson who is basically one of the inventors of HTTP, and he designed the API so it is easy for you to follow the HTTP standard, e.g. generating standards-compliant headers

2- is in the .Net framework 4.5, so it has some guaranteed level of support for the forseeable future

3- also has the xcopyable/portable-framework version of the library if you want to use it on other platforms - .Net 4.0, Windows Phone etc.

so im gonna show you an exmaple of using HttpClient:

        var uri = "";

        var client = new HttpClient();
            var values = new List<KeyValuePair<string, string>>();

            // add values to data for post
            values.Add(new KeyValuePair<string, string>("FirstName", "KRITZTE"));
            FormUrlEncodedContent content = new FormUrlEncodedContent(values);

            // Post data
            var result = client.PostAsync(uri, content).Result;

            // Acess content as stream which you can read into some string

            //Access the result status code
        catch(AggregateException ex)
            //get all possible exceptions which are thrown
            foreach (var item in ex.Flatten().InnerExceptions)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download