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


  • 相关阅读:
    全字母短句
    java 遍历map的方法
    实现num1、num2交换,无中间变量
    N多条短信,用什么算法从中找出相似内容的来?
    Linux基础_磁盘分区
    Linux基础_软链接,硬链接
    Linux基础_系统启动流程
    Linux基础_合并,归档,压缩,dump,编辑器
    Linux基础_Linux操作系统简介
    计算机基础_操作系统
  • 原文地址:https://www.cnblogs.com/yanlixin/p/840600.html
Copyright © 2011-2022 走看看