當我試圖從HttpClient
(MVC應用程序)調用GetAllFiles()
時,它卡在結束大括號後。我可以在調試模式下看到數據,但是當到達時,結束括號就會卡住。WebApi無法返回到HttpClient
當我從postman調用相同的WebApi方法時,我可以輕鬆地獲取數據。
public class FileToDriveController : ApiController
{
public IHttpActionResult GetAllFiles()
{
//Gets credentials
GoogleUtility googleUtility = new GoogleUtility();
UserCredential credential = googleUtility.GetCredential();
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "DriveApiFileUpload"
});
var listRequest = service.Files.List();
listRequest.MaxResults = 10;
IList<Google.Apis.Drive.v2.Data.File> files = listRequest.Execute().Items;
if(files!=null)
{
return Ok(files); //i can see files here in debug mode
}
return InternalServerError();
}
}
這是HttpClient的的調用API
public async Task<HttpResponseMessage> getAllFiles()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:16184/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = await client.GetAsync("api/FileToDrive/GetAllFiles");
if (message.StatusCode == HttpStatusCode.OK)
{
return message;
}
return null;
}
}
謝謝伊蘭。這真的幫了我很多。鏈接到博客幫了我很多。 –
當我第一次遇到這個問題時,我很頭疼,試圖解決這個問題,很高興我能幫上忙! – PartlyCloudy