2017-02-20 154 views
0
public const string CloudApi = "https://www.idxhome.com/restServices/cloud-idx/login"; 
public const string password = "cloudidx"; 
public const string clientId = "65912"; 
public async Task<ActionResult> Index() 
{ 
    HttpClient client = new HttpClient(); 
    client.BaseAddress = new Uri(CloudApi); 
    client.DefaultRequestHeaders.Accept.Clear(); 

    var values = new Dictionary<string, string> 
    { 
     { "clientId", "cloudidx" }, 
     { "password", "65912" } 
    }; 

    var content = new FormUrlEncodedContent(values); 
    HttpResponseMessage response = await client.PostAsync(CloudApi,content); 

    if (response.IsSuccessStatusCode) 
    { 
     var responseString = response.Content.ReadAsStringAsync().Result; 

     response.Content.ToString(); 
    } 

    client.Dispose(); 

    return View(); 
} 

我試過使用HttpWebRequest以及httpClient。 我在這裏看了大約十幾個問題,他們都沒有幫助過這種情況。C#httpClient不返回內容

我可以使用PHP和Curl並獲得我期待的結果。 PHP中返回的數據是JSON鏈接列表。看起來像這樣..

"links":[ 
    { 
     "rel":"search", 
     "href":"http://www.idxhome.com/restServices/cloud-idx/client/2/listing-search/MTM4NDMwMDg2MTA3ODoxYjExMWVhNDVlYWVmMjdiOTZhNTE5NjBhMjU3YzYzMzNhZmI0MzkwODk2MmExY2U0NU0ZjFiOGE3YzFhMTU4MjYxNjNlZjNhYjF-hZWFmNDI2ZWE3NmQwMjE4ODdjNmMzNGQxZmIxYTE4MGQ2MjUyM2YZWNhYjAwM2Q5MWFmNzgyYzM3NzcwYzFmNDk5OGM1Y2ExNDZhYjQwMDk2OWI4NmFhYTllZj..." 
    }, 
    { 
     "rel":"detail", 
     "href":"http://www.idxhome.com/restServices/cloud-idx/client/2/listing-search/MTM4NDMwMDg2MTA3ODoxYjExMWVhNDVlYWVmMjdiOTZhNTE5NjBhMjU3YzYzMzNhZmI0MzkwODk2MmExY2U0NU0ZjFiOGE3YzFhMTU4MjYxNjNlZjNhYjF-hZWFmNDI2ZWE3NmQwMjE4ODdjNmMzNGQxZmIxYTE4MGQ2MjUyM2YZWNhYjAwM2Q5MWFmNzgyYzM3NzcwYzFmNDk5OGM1Y2ExNDZhYjQwMDk2OWI4NmFhYTllZj..." 
    }, 
    { 
     "rel":"cities", 
     "href":"http://www.idxhome.com/restServices/cloud-idx/client/2/listing-search/MTM4NDMwMDg2MTA3ODoxYjExMWVhNDVlYWVmMjdiOTZhNTE5NjBhMjU3YzYzMzNhZmI0MzkwODk2MmExY2U0NU0ZjFiOGE3YzFhMTU4MjYxNjNlZjNhYjF-hZWFmNDI2ZWE3NmQwMjE4ODdjNmMzNGQxZmIxYTE4MGQ2MjUyM2YZWNhYjAwM2Q5MWFmNzgyYzM3NzcwYzFmNDk5OGM1Y2ExNDZhYjQwMDk2OWI4NmFhYTllZj..." 
    }, 
    { 
     "rel":"zips", 
     "href":"http://www.idxhome.com/restServices/cloud-idx/client/2/listing-search/MTM4NDMwMDg2MTA3ODoxYjExMWVhNDVlYWVmMjdiOTZhNTE5NjBhMjU3YzYzMzNhZmI0MzkwODk2MmExY2U0NU0ZjFiOGE3YzFhMTU4MjYxNjNlZjNhYjF-hZWFmNDI2ZWE3NmQwMjE4ODdjNmMzNGQxZmIxYTE4MGQ2MjUyM2YZWNhYjAwM2Q5MWFmNzgyYzM3NzcwYzFmNDk5OGM1Y2ExNDZhYjQwMDk2OWI4NmFhYTllZj..." 
    } 
    ], 
"cloudIdxSearchProfile":{ 
    "bedrooms":2, 
    "cityId":"274,284", 
    "fullBaths":1, 
    "lotAcres":0, 
    "maxListPrice":1000000, 
    "minListPrice":0, 
    "newConstructionYn":false, 
    "openHomesOnlyYn":false, 
    "propertyType":"SFR,CND", 
    "squareFeet":0, 
    "zip":"94702,94703" 
}, 
"cloudIdxDetailProfile":{ 
    "listingNumber":"88873733", 
    "boardId":6 
}, 
"message":"Include 'idxCloudSearchProfile' url parameters with requests to the 'search' URL. For 'detail' requests, include 'idxCloudDetailProfile' url parameters."` 

API I am trying to utilize。它以成功登錄,但我無法獲取登錄呼叫所傳遞的數據。

我相信curl調用和httpclient調用有所不同,但我不確定。

任何幫助表示讚賞。 我已編輯的代碼以下,但響應字符串仍然是空Copy of debug view

 public async Task<ActionResult> Index() 
    { 
     using (var client = new HttpClient()) { 
     client.BaseAddress = new Uri(CloudApi); 
     client.DefaultRequestHeaders.Accept.Clear(); 
     //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     var values = new Dictionary<string, string> 
     { 
      { "clientId", "65912" }, 
      { "password", "cloudidx" } 

     }; 
     var content = new FormUrlEncodedContent(values); 
     HttpResponseMessage response = await client.PostAsync("login", content); 
      string responseString = String.Empty; 
      if (response.IsSuccessStatusCode) 
     { 
      responseString = await response.Content.ReadAsStringAsync(); 

     } 
    } 
     return View(); 
    } 

這裏是捲曲呼叫工作

function cloudIDXCall($url, $method, $data = array()) { 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // return response as a string 
curl_setopt($curl, CURLOPT_SSLVERSION, 3); // force default SSL version to 3  
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // turn off verification of the remote server's certificate - helps with Windows 

$queryString = http_build_query($data); // pack parameters into a URL query string 

if ($method == 'get') { 
    // GET is the default request method for cURL 
    if (strlen($queryString) > 0) $url = $url . '?' . $queryString; // append parameters for GET 
} elseif ($method == 'post') { 
    curl_setopt($curl, CURLOPT_POST, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $queryString); // set parameters for POST 
} else { 
    return array('failure' => 'Invalid method'); 
} 

curl_setopt($curl, CURLOPT_URL, $url); 
$response = curl_exec($curl); 

if (!$response) { 
    $errMsg = "Error: " . curl_error($curl) . " - Code: " . curl_errno($curl); 
    curl_close($curl); 
    return array('failure' => $errMsg); 
} 

$responseArray = json_decode ($response, $assoc = true); // decode JSON into assoc array 
curl_close($curl); 
return $responseArray; 

}

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; solved the issue... Not sure why it made a difference but it did. 
+0

你嘗試await'加上''到ReadAsStringAsync'? –

+0

我沒有看到任何顯示結果的代碼,或者將它們放入View中有權訪問的變量(例如ViewBag)中。你確定數據沒有被實際返回,你只是沒有正確顯示它? –

+0

不要在asp.net中使用.Result。谷歌斯蒂芬Cleary並且找出原因。 – Crowcoder

回答

0

用途:

var responseString = await response.Content.ReadAsStringAsync(); 

結果將在responseString

而且更一個問題:

您設定BaseAddressCloudApi,所以現在當你調用PostAsync應該是空白requestUri。像這樣:

var response = await client.PostAsync(String.Empty, content); 

也請你幫個忙,並使用C#提供的using關鍵字。

因此,這裏是你的完整的方法應該是什麼樣子:

using(var client = new HttpClient()) 
    { 
     client.BaseAddress = "http://baseApiAddress.com"; 

     var values = new Dictionary<string, string> 
     { 
      { "clientId", "cloudidx" }, 
      { "password", "65912" } 
     }; 

     var content = new FormUrlEncodedContent(values); 
     var response = await client.PostAsync("apiMethod", content); 
     string responseString = String.Empty; 

     if(response.IsSuccessStatusCode) 
     { 
      responseString = await response.Content.ReadAsStringAsync(); 
     } 
    } 

    return View(); 
+0

試過這個無濟於事... responseString仍然是空的。 –

+0

你在響應中獲得什麼狀態碼? –

+0

其給我的狀態200好 –