zoukankan      html  css  js  c++  java
  • 脚本的力量:MSDN中一段代码的IronPython与PowerShell实现

    下面的示例说明如何创建 WebRequest 实例并返回响应。 :-)
    ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref10/html/T_System_Net_WebRequest.htm
    C#
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    namespace Examples.System.Net
    {
        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 ();
            }
        }
    }

    IronPython

    from System import *
    from System.IO import *
    from System.Net import *
    from System.Text import *
    request = WebRequest.Create ("http://www.contoso.com/default.html")
    response = request.GetResponse ()
    Console.WriteLine (response.StatusDescription)
    dataStream = response.GetResponseStream ()
    reader = StreamReader (dataStream)
    responseFromServer = reader.ReadToEnd ()
    Console.WriteLine (responseFromServer)
    reader.Close ()
    dataStream.Close ()
    response.Close ()

    PowerShell

    $request = [System.Net.WebRequest]::Create("http://www.contoso.com/default.html");
    $request.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    $response = $request.GetResponse()
    [System.Console]::WriteLine($response.StatusDescription)
    $dataStream = $response.GetResponseStream()
    $reader = New-Object System.IO.StreamReader($dataStream)
    $responseFromServer = $reader.ReadToEnd()
    [System.Console]::WriteLine($responseFromServer)
    $reader.Close()
    $dataStream.Close()
    $response.Close()


  • 相关阅读:
    codevs2606 约数和问题
    UOJ150 运输计划
    codevs1279 Guard 的无聊
    codevs1997 守卫者的挑战
    codevs1291 火车线路
    codevs1217 借教室
    codevs1281 Xn数列
    codevs1218 疫情控制
    codevs1199 开车旅行
    BZOJ1941 [Sdoi2010]Hide and Seek
  • 原文地址:https://www.cnblogs.com/yanlixin/p/840600.html
Copyright © 2011-2022 走看看