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()


  • 相关阅读:
    表达式求值
    火柴排队(归并)
    POJ 3254 压缩状态DP
    ZOJ 3471 压缩状态DP
    Boost IPC Persistence Of Interprocess Mechanisms 例子
    TCO 2014 Round 1A
    Google Code Jam 2014 Qualification 题解
    STL set_difference set_intersection set_union 操作
    b_zj_特征提取(map记录上一个特征运动的次数)
    b_zj_最大连续的相同字符子串的长度(双指针+找突破点)
  • 原文地址:https://www.cnblogs.com/yanlixin/p/840600.html
Copyright © 2011-2022 走看看