如何从WCF Web服务发出http GET请求

时间:2014-04-06 23:49:03

标签: .net wcf

有很多关于如何向WCF服务发出GET请求的信息 但我需要从一个WCF Web服务发出一个http GET请求到一些提供我需要的服务的第三方服务器 那么,我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

使用.net WebRequest对象:

public class WebRequestGetExample
    {
        public static void Main ()
        {
            // 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 ();
        }
    }