Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient - UploadValues : Get Status Response

Tags:

c#

webclient

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;
}
like image 690
locknies Avatar asked Oct 01 '15 06:10

locknies


1 Answers

You should not use the WebClient because of 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 I'm gonna show you an example of using HttpClient:

var uri = "http://google.com";
        
var client = new HttpClient();
try
{
   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 = await client.PostAsync(uri, content);

   // Access content as stream which you can read into some string
   Console.WriteLine(result.Content);

   // Access the result status code
   Console.WriteLine(result.StatusCode);
}
catch(AggregateException ex)
{
    // get all possible exceptions which are thrown
    foreach (var item in ex.Flatten().InnerExceptions)
    {
        Console.WriteLine(item.Message);
    }
}
like image 184
SHM Avatar answered Oct 21 '22 18:10

SHM