是否可以从Web服务.asmx调用rest api?

时间:2018-05-22 18:56:47

标签: .net web-services

我正在使用Web服务编写遗留代码。我必须从网络服务下面的DAL呼叫第三方休息api。 我们还有dot net framework 4.0,它不支持httpclient。

Plesae让我知道是否可以这样做?

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。请查看WebRequest classHttpWebRequest class

示例(摘自第一个链接文章):

// Create a request for the URL.        
WebRequest request = WebRequest.Create("http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
Console.WriteLine(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
相关问题