2

I have to authenticate and then request a Web Api in asp.net. The authentication is completed properly. But when I pass the session values to a new request to generate a certain response, the response returns an Controller not found error. I am trying to implement it in Asp.Net MVC application.

Here is the code for the main Index:

public async Task<ActionResult> Index()
    {
        var result = "";
        string json = " { \"username\": \"webservice\", \"password\": \"iIU2piH6wpBF92T\" }";
        var request = (HttpWebRequest)WebRequest.Create("https://os.mcparcel.com/osapi/user/login.json");

        request.Method = "POST";
        request.UserAgent = "MCParcel Plugin";
        request.ContentType = "application/json";

        using (var s = request.GetRequestStream())
        {
            using (var stw = new StreamWriter(s))
            {
                stw.Write(json);
            }
        }

        try
        {
            var response = (HttpWebResponse)request.GetResponse();
            var data = new StreamReader(response.GetResponseStream());
            result = data.ReadToEnd();//<-this gives the value for session id and name

            string key = "WucHEwRuy7trUDE7u3agEqEWrUkajuCr";
            string order_id = "MCParcel3";
            int labels_number = 1;


            var r = await Test(key, order_id, labels_number, result);
        }
        catch (WebException ex)
        {
            var errorData = new StreamReader(ex.Response.GetResponseStream());
            var errorString = errorData.ReadToEnd();

        }

        return View();

    }

Here are the other functions inside the same controller, the error is in:

var response

 public static async Task<string> Test(string key, string order_id, int labels_number, dynamic results, int? shipment_id = 0)
    {
        var r = await GetShippingOptionRequest(key, order_id, labels_number, results);
        Console.WriteLine(r);
        return r;
    }


  public static async Task<string> GetShippingOptionRequest(string key, string order_id, int labels_number, dynamic results, int? shipment_id = 0)
    {
        string json = " { \"key\": \"" + key + "\", \"order_id\": \"" + order_id + "\", \"labels_no\": " + labels_number + ", \"shipment_id\": " + shipment_id + "";


        var dynObj = JsonConvert.DeserializeObject<LoginResponse>(results);

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Method", "POST");
            client.DefaultRequestHeaders.Add("ContentType", "application/json");
            client.DefaultRequestHeaders.Add("UserAgent", "MCParcel Plugin");

            client.DefaultRequestHeaders.Add("sessid", dynObj.sessid);
            client.DefaultRequestHeaders.Add("session_name", dynObj.session_name);

            client.DefaultRequestHeaders.Add("key", key);
            client.DefaultRequestHeaders.Add("order_id", order_id);
            client.DefaultRequestHeaders.Add("labels_number", Convert.ToString(labels_number));
            client.DefaultRequestHeaders.Add("shipment_id", Convert.ToString(shipment_id));


            //the code below is the required response that is not returning values and returning 404 error
            var response = await client.GetStringAsync("https://os.mcparcel.com/osapi/service_os_api/get_label_info.json");
            return response;
        }
    }

The response should return something similar to following:

{ "source_labels": ["https://os.mcparcel.com/sites/os.mcparcel.com/files/sourcepdflabels/labels_8c1e3033a8d23c632006639f39ef6964.pdf"], "tracks": ["https://os.mcparcel.com/track_and_trace/(J)JD0000900581338100135004"], "labels_file": "https://os.mcparcel.com/sites/os.mcparcel.com/files/pdf_labels/70994c37ad2a99d4047e0684c3e05c1f.pdf" }

Any help will be highly appreciated. Thanks in advance!!!

1 Answers1

1

Are you able to hit other controllers? If yes, then you might try to create another controller and start moving your current code with simple json request and build it up to complex json request. Or it might also be your routing.

If no, then it might be you have not setup config.MapHttpAttributeRoutes().

alltej
  • 6,787
  • 10
  • 46
  • 87
  • I have already setup 'config.MapHttpAttributeRoutes()' and i am able to hit other controllers. How do you build up to complex json request? – Prasanna Man Rajbanshi Jun 29 '16 at 04:52
  • Are you able to hit the controller or the API with a REST client like Postman? If not then your URL is not correct and it has nothing to do with your client code. – alltej Jun 29 '16 at 18:46
  • Well, i found the problem and solved it. It had to do with cookies. I removed the httpclient methods with the webrequest methods which included cookies and hence solved the problem!! @Allan thanks for your concern though! – Prasanna Man Rajbanshi Jun 30 '16 at 03:53