2016-06-28 127 views
2

我必須在asp.net中進行身份驗證,然後請求Web Api。身份驗證已正確完成。但是當我將會話值傳遞給一個新的請求來產生一定的響應時,響應返回一個控制器未找到的錯誤。我試圖在Asp.Net MVC應用程序中實現它。Asp.Net中的HttpClient響應返回404(未找到:找不到控制器)

這裏是主索引的代碼:

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(); 

    } 

下面是在同一控制器內的其它功能,誤差是:

變種響應

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; 
     } 
    } 

響應應該返回類似的東西,以下列:

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

任何幫助將不勝感激。提前致謝!!!

+0

您確定您使用的是正確的網址嗎? –

+0

我很確定。 @DanielStackenland –

+1

爲什麼要將真實的憑據展現給世界? – haim770

回答

1

你能擊中其他控制器嗎?如果是的話,那麼你可以嘗試創建另一個控制器,並開始移動你的當前代碼與簡單的JSON請求,並建立它複雜的JSON請求。或者它也可能是你的路由。

如果不是,那麼它可能是你沒有設置config.MapHttpAttributeRoutes()

+0

我已經設置'config.MapHttpAttributeRoutes()',我可以擊中其他控制器。你如何構建複雜的json請求? –

+0

您是否能夠像Postman一樣使用REST客戶端來訪問控制器或API?如果沒有,那麼你的URL是不正確的,它與你的客戶端代碼無關。 – alltej

+0

嗯,我發現了問題並解決了它。這與Cookie有關。我使用包含cookie的webrequest方法刪除了httpclient方法,因此解決了問題! @Allan非常感謝您的關注! –