ASP.NET Web API调用时出现: 基础连接已经关闭: 发送时发生错误

以下是调用API的页面, 用的是Razor的写法, 我测试了一下是在执行Stream dataStream = client.OpenRead(GetURL); 这句话的时候报错的, 错误信息是"基础连接已经关闭: 发送时发生错误", 但是我单独打开API是没有问题的, API地址是: https://webapi20200813111324....{parameter}, 后面的{parameter}可以是任何字符串.

@using Newtonsoft.Json;

@{

bool isConnectFailed = false;

string errorInfo = "error";

List<string> cards = new List<string>();

var playerName = Request["PlayName"];

if (IsPost)

{

string GetURL = "https://webapi20200813111324.azurewebsites.net/api/HandOfCards/" + playerName;

WebClient client = new WebClient();

try

{

Stream dataStream = client.OpenRead(GetURL);

StreamReader reader = new StreamReader(dataStream);

var results = JsonConvert.DeserializeObject<dynamic>(reader.ReadLine());

reader.Close();

foreach (var item in results)

{

cards.Add((string)item.imageLink);

}

}

catch (System.Net.WebException e)

{

isConnectFailed = true;

errorInfo = e.Message;

}

}

}

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8" />

<style>

body {

font-family: Verdana;

margin-left: 50px;

margin-top: 50px;

}

div {

border: 1px solid black;

width: 800px;

margin: 1.2em;

padding: 1em;

}

</style>

<title>BensCards: Deal yourself a hand.</title>

</head>

<body>

@if (IsPost)

{

<label id="labelGoal">Which player has the best hand.</label>

<br />

<div>

@if (isConnectFailed)

{

<p><label id="labelPlayer1">Web API Connection failed, Message: @errorInfo</label></p>

}

else

{

<p><label id="labelPlayer1">Player1: @playerName</label></p>

foreach (string card in cards)

{

<img width="75" height="100" alt="cardImage" src="https://deckofcards20200804.blob.core.windows.net/carddeck/@card" />

}

}

</div>

<label id="errorMessageLabel" />

}

else

{

<label id="labelGoal">

Enter the players name and deal the cards.

</label>

<br /><br />

<form method="post">

<div>

<p>Player 1: @Html.TextBox("PlayerName")</p>

<p><input type="submit" value="Deal Cards" class="submit" /></p>

</div>

</form>

}

</body>

</html>

以下是错误截图:

回答

https://www.jianshu.com/p/370...

以上是 ASP.NET Web API调用时出现: 基础连接已经关闭: 发送时发生错误 的全部内容, 来源链接: utcz.com/a/38858.html

回到顶部