0

I cannot pass value properly. This code executed successfully but the null value reached to the specified page("http://.............).

Product objProduct = new Product();
objProduct.id = "1";
objProduct.name = "Sana";
string json = JsonConvert.SerializeObject(objProduct);

var baseAddress = "http://..................";

var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";

string parsedContent = json;
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);

Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();

var response = http.GetResponse();

var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
Alexis Tyler
  • 1,515
  • 6
  • 26
  • 44
Ajisha
  • 47
  • 8

2 Answers2

1

Can you use Microsoft.Net.Http

using (var client = new HttpClient()) 
{
    var objProduct = new Product();
    objProduct.id = "1";
    objProduct.name = "Sana";
    string json = JsonConvert.SerializeObject(objProduct);
    var content = new StringContent(json);
    var result = await client.PostAsync("http://localhost/product/", content);
    var responseAsString = await result.Content.ReadAsStringAsync();
}
jjchiw
  • 4,018
  • 27
  • 30
  • What is the deffrent between HttpClient and WebClient ? – malbarmavi Feb 27 '17 at 08:50
  • 1
    @Muhammad it's a question that it's answered here http://stackoverflow.com/questions/20530152/need-help-deciding-between-httpclient-and-webclient/27737601#27737601 – jjchiw Feb 27 '17 at 08:55
  • hav you any solution for my problem – Ajisha Feb 27 '17 at 10:23
  • @Ajisha since you could installed the package you should add to the method the `async` keyword.... but I'm thinking that as @PatrickHofman said the answer is not the correct and it does not helps you because you will need to modify the methods to work with asynchronous programming https://msdn.microsoft.com/library/hh191443(vs.110).aspx – jjchiw Feb 27 '17 at 10:25
  • If you're using .net framework 4 you should not be able to install the package since it works from >= 4.5, Modify your question and add the way you deserialize the response – jjchiw Feb 27 '17 at 10:58
0

Another simple solution that I use to make request is WebClient and your code can be like this

  Product objProduct = new Product();
  objProduct.id = "1";
  objProduct.name = "Sana";
  string json = JsonConvert.SerializeObject(objProduct);
  var baseAddress = "http://..................";

  var respond = "";

  //Post request 
  using (WebClient wc = new WebClient())
  {
    wc.Encoding = Encoding.UTF8;
    respond =  wc.UploadString(baseAddress, json);
  }

the respond variable here will have the result of API return

malbarmavi
  • 18,795
  • 4
  • 45
  • 73
  • the respond variable hold {"products":[{"id":null,"name":null}],"success":1} – Ajisha Feb 27 '17 at 08:52
  • during the execution joson variable shows {"id":"1","name":"Sana"}. But after execution the responds hold nul value.i couldnt find the reason – Ajisha Feb 27 '17 at 08:58
  • It 's depend of the return from you services , what is the return statement in your services @Ajisha – malbarmavi Feb 27 '17 at 09:01
  • "It 's depend of the return from you services , what is the return statement in your services " what you mean by service – Ajisha Feb 27 '17 at 09:09
  • Return data from a php server. But i can receive data from that server. That code executed properly. – Ajisha Feb 27 '17 at 09:30
  • would you please give me a solution for my problem – Ajisha Feb 27 '17 at 10:22
  • The UploadString Uploads the specified string to the specified resource, using the POST method,and return string containing the response sent by the server. so the result is handle by the service @Ajisha – malbarmavi Feb 27 '17 at 11:15
  • Is your api service public so I can look at ?? @Ajisha – malbarmavi Feb 27 '17 at 11:18
  • that code is working. The problem is in the server side code. so i fix that error. Then both the solutions executed properly..thank you.... – Ajisha Feb 27 '17 at 11:50