0

I have to accesss an get some data from one of API.This API has only mentoned this method.No documentation showing how to acess and all.Data will be returned as Json.

This is the product API

1. GetAllProducts
POST
http://api.domain.com/api/Products/All
Content-Type: application/json; charset=utf-8
{"Token":"EncryptedToken"}

I have generated the Token and I tried to access it like this.But nothing is recieving.I found an example and tried to do like this.May be this is wrong.Can any one show me how to access and get the data.Please see my code below and thanks in advanced.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Web.Script;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Text;



public partial class Products : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebRequest request = WebRequest.Create("http://api.domain.com/api/Products/All?Token=U5Y1oPjI4DqwZgZkp-pVSmbIpP9XuXquKGYuREGwSNDX5OUhHAQVzI3exVOVzDD3|qlFREqiRHuKDG3gN5Zk05M5YjtWOhD8A11oxT7wolQ0gSLyKzXxNHgj94idAm4Wy6|CVnC2pguIbugAfqxSSJvf1KE_");
        request.Method = "POST";


        string postData = "Data to post here";

        byte[] post = Encoding.UTF8.GetBytes(postData);


        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = post.Length;
        Stream reqdataStream = request.GetRequestStream();

        reqdataStream.Write(post, 0, post.Length);
        reqdataStream.Close();

        request.Credentials = CredentialCache.DefaultCredentials;

        WebResponse response = null;
        try
        {

            response = request.GetResponse();

        }
        catch (Exception ex)
        {
            Response.Write("Error Occured.");
        }
    }
}
user1353519
  • 71
  • 2
  • 12
  • This helped me to solve my issue http://stackoverflow.com/questions/1502500/how-to-use-webrequest-to-post-data-and-get-response-from-a-webpage – user1353519 Mar 16 '14 at 05:04

1 Answers1

0

I think you should change these things:

content type

 request.ContentType = "application/json"; 

the post data

 string postData = "{\"Token\":\"U5Y1oPjI4DqwZgZkp-pVSmbIpP9XuXquKGYuREGwSNDX5OUhHAQVzI3exVOVzDD3|qlFREqiRHuKDG3gN5Zk05M5YjtWOhD8A11oxT7wolQ0gSLyKzXxNHgj94idAm4Wy6|CVnC2pguIbugAfqxSSJvf1KE_\"}";

the URI

WebRequest request = WebRequest.Create("http://api.domain.com/api/Products/All");
xavigonza
  • 170
  • 1
  • 7
  • I tried your code.No errors but I dont get any data from API? – user1353519 Mar 15 '14 at 17:08
  • read the stream within the response: Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader (dataStream); string responseFromServer = reader.ReadToEnd (); – xavigonza Mar 15 '14 at 19:09