{"componentChunkName":"component---src-templates-post-template-js","path":"/powershell-get-http-response-en","result":{"data":{"markdownRemark":{"id":"a9efb735-080e-5344-9fc9-37075af9c2e6","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/powershell-get-http-response\">original page</a>.</p>\n</blockquote>\n<h2 id=\"powershell-and-net-framework\" style=\"position:relative;\"><a href=\"#powershell-and-net-framework\" aria-label=\"powershell and net framework permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>PowerShell and .NET Framework</h2>\n<p>PowerShell is a powerful shell developed as open source that runs on Windows, Linux, and macOS, and is a script language designed as an object-oriented language.</p>\n<p>PowerShell is based on the .NET Framework.<br>\nCommands, which are equivalent to commands in Bash and other shells, are called cmdlets, but their actual substance is .NET Framework objects.</p>\n<p>Like shells used on Linux and elsewhere, it is possible to pass processing results using pipes, but the information passed is not text but .NET Framework objects.<br>\nTherefore, flexible and powerful processing can be realized.</p>\n<p>In the next section, we will actually load .NET Framework objects from PowerShell.</p>\n<h2 id=\"referencing-systemnetwebclient\" style=\"position:relative;\"><a href=\"#referencing-systemnetwebclient\" aria-label=\"referencing systemnetwebclient permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Referencing System.Net.WebClient</h2>\n<p>For example, just by executing the following script in PowerShell, the $content variable will contain the HTTP response from the specified URL.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\"><span class=\"token function\">Add-Type</span> <span class=\"token operator\">-</span>AssemblyName System<span class=\"token punctuation\">.</span>Net<span class=\"token punctuation\">.</span>Http\n<span class=\"token variable\">$webClient</span> = <span class=\"token function\">New-Object</span> System<span class=\"token punctuation\">.</span>Net<span class=\"token punctuation\">.</span>Http<span class=\"token punctuation\">.</span>HttpClient\n<span class=\"token variable\">$content</span> = <span class=\"token variable\">$webClient</span><span class=\"token punctuation\">.</span>GetAsync<span class=\"token punctuation\">(</span><span class=\"token string\">\"https://kashiwaba-yuki.com\"</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Specifically, first we use the Add-Type cmdlet, which can define .NET Framework classes, to reference the System.Net.Http class.<br>\nNext, we store an instance of the System.Net.Http class in $webClient and use the GetAsync method to retrieve the HTTP response from the specified URL.</p>\n<p>When we actually output this $content, the following results were confirmed.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$content\n\nResult                 : StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\n                         {\n                           Transfer-Encoding: chunked\n                           Connection: keep-alive\n                           Link: https://kashiwaba-yuki.com/wp-json/; rel=\"https://api.w.org/\"\n                           Vary: Range\n                           Vary: Accept-Encoding\n                           X-Cache: MISS\n                           Date: Wed, 01 Jul 2020 07:38:37 GMT\n                           Server: Apache\n                           X-Powered-By: PHP/7.4.4\n                           Content-Type: text/html; charset=UTF-8\n                         }\nId                     : 365\nException              :\nStatus                 : RanToCompletion\nIsCanceled             : False\nIsCompleted            : True\nCreationOptions        : None\nAsyncState             :\nIsFaulted              : False\nAsyncWaitHandle        : System.Threading.ManualResetEvent\nCompletedSynchronously : False</code></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>So I’ve summarized that you can reference .NET Framework objects from PowerShell.</p>\n<p>Being able to reference .NET Framework objects means that, to put it extremely, what you can do with C# can also be implemented in PowerShell.<br>\nSince you can call C# programs from PowerShell, you might not need to write everything in PowerShell…</p>\n<h2 id=\"references\" style=\"position:relative;\"><a href=\"#references\" aria-label=\"references permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>References</h2>\n<ul>\n<li><a href=\"https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=netcore-3.1\" target=\"_blank\" rel=\"noopener noreferrer\">WebClient Class (System.Net) | Microsoft Learn</a></li>\n<li><a href=\"https://forsenergy.com/ja-jp/windowspowershellhelp/html/7c6ad475-d556-436e-841b-7e618f888644.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Add-Type</a></li>\n</ul>","fields":{"slug":"/powershell-get-http-response-en","tagSlugs":["/tag/power-shell-en/","/tag/windows-en/","/tag/english/"]},"frontmatter":{"date":"2020-07-01","description":"This article summarizes how to reference .NET Framework objects from PowerShell.","tags":["PowerShell (en)","Windows (en)","English"],"title":"Getting HTTP Responses by Referencing .NET Framework Objects from PowerShell","socialImage":{"publicURL":"/static/dc4d8b7f8795f3c3d3489d9957d155f2/no-image.png"}}}},"pageContext":{"slug":"/powershell-get-http-response-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}