【Azure 环境】各种语言版本或命令,发送HTTP/HTTPS的请求合集
问题描述
写代码的过程中,时常遇见要通过代码请求其他HTTP,HTTPS的情况,以下是收集各种语言的请求发送,需要使用的代码或命令
一:PowerShell
Invoke-WebRequest https://docs.azure.cn/zh-cn/
二:curl
curl https://docs.azure.cn/zh-cn/
命令说明:https://curl.haxx.se/docs/httpscripting.html
三:C#
//添加Http的引用
using System.Net.Http;
//使用HttpClient对象发送Get请求
using (HttpClient httpClient = new HttpClient())
{
var url = $"https://functionapp120201013155425.chinacloudsites.cn/api/HttpTrigger1?name={name}";
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
httpRequest.Headers.Add("Accept", "application/json, text/plain, */*"); var response = httpClient.SendAsync(httpRequest).Result;
string responseContent = response.Content.ReadAsStringAsync().Result; return responseContent;
} //POST
using (HttpClient httpClient = new HttpClient())
{
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, botNotifySendApi);
httpRequest.Headers.Add("Accept", "application/json, text/plain, */*");
httpRequest.Headers.Add("Authorization", apimauthorization);
var content = new StringContent(messageBody, Encoding.UTF8, "application/json");
httpRequest.Content = content; var response = await httpClient.SendAsync(httpRequest);
string responseContent = await response.Content.ReadAsStringAsync();
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
//responseContent
}
}
//POST 2
using (HttpClient httpClient = new HttpClient())
{
string messageBody = "{\"vehicleType\": \"train\",\"maxSpeed\": 125,\"avgSpeed\": 90,\"speedUnit\": \"mph in code\"}";
var url = $"https://test02.azure-api.cn/echo/resource";
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, url);
httpRequest.Headers.Add("Accept", "application/json, text/plain, */*"); var content = new StringContent(messageBody, Encoding.UTF8, "application/json");
httpRequest.Content = content; var response = httpClient.SendAsync(httpRequest).Result;
responseContent = response.Content.ReadAsStringAsync().Result;
}
代码说明:https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1
四:Java
pom.xml <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
GET/POST
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class HttpClientExample { // one instance, reuse
private final CloseableHttpClient httpClient = HttpClients.createDefault(); public static void main(String[] args) throws Exception { HttpClientExample obj = new HttpClientExample(); try {
System.out.println("Send Http GET request");
obj.sendGet(); System.out.println("Send Http POST request");
obj.sendPost();
} finally {
obj.close();
}
} private void close() throws IOException {
httpClient.close();
} private void sendGet() throws Exception { HttpGet request = new HttpGet("https://docs.azure.cn/zh-cn/"); // add request headers
// request.addHeader("customkey", "test");try (CloseableHttpResponse response = httpClient.execute(request)) { // Get HttpResponse Status
System.out.println(response.getStatusLine().toString()); HttpEntity entity = response.getEntity();
Header headers = entity.getContentType();
System.out.println(headers); if (entity != null) {
// return it as a String
String result = EntityUtils.toString(entity);
System.out.println(result);
} } } private void sendPost() throws Exception { HttpPost post = new HttpPost("https://httpbin.org/post"); // add request parameter, form parameters
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("username", "test"));
urlParameters.add(new BasicNameValuePair("password", "admin"));
urlParameters.add(new BasicNameValuePair("custom", "test")); post.setEntity(new UrlEncodedFormEntity(urlParameters)); try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post)) { System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
五:Python
import requests
x = requests.get('https://docs.azure.cn/zh-cn/')
print(x.text)
代码说明:https://www.w3schools.com/python/module_requests.asp
六:PHP
//Additionally consider two more PHP functions that can be coded in a single line. $data = file_get_contents ($my_url); //This will return the raw data stream from the URL. $xml = simple_load_file($my_url);
curl in PHP
// create & initialize a curl session
$curl = curl_init(); // set our url with curl_setopt()
curl_setopt($curl, CURLOPT_URL, "api.example.com"); // return the transfer as a string, also with setopt()
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // curl_exec() executes the started curl session
// $output contains the output string
$output = curl_exec($curl); // close curl resource to free up system resources
// (deletes the variable made by curl_init)
curl_close($curl);
代码说明: https://weichie.com/blog/curl-api-calls-with-php/
七:JavaScript
var request = new XMLHttpRequest()
request.open('GET', 'https://ghibliapi.herokuapp.com/films', true)
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response)
if (request.status >= 200 && request.status < 400) {
data.forEach((movie) => {
console.log(movie.title)
})
} else {
console.log('error')
}
}
request.send()
代码说明:https://www.taniarascia.com/how-to-connect-to-an-api-with-javascript/
八:jQuery.ajax()
var menuId = $( "ul.nav" ).first().attr( "id" );
var request = $.ajax({
url: "script.php",
method: "POST",
data: { id : menuId },
dataType: "html"
}); request.done(function( msg ) {
$( "#log" ).html( msg );
}); request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
代码说明: https://api.jquery.com/jquery.ajax/
jQuery 2:
<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
var request = $.ajax({
url: "https://test01.azure-api.cn/echo/resource",
type: "POST",
headers: {
"x-zumo-application": "test"
},
data: {
vehicleType: "train",
maxSpeed: 125,
avgSpeed: 90,
speedUnit: "mph"
},
dataType: "text"
});
request.done(function (msg) {
console.log(msg);
});
request.fail(function (jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
});
</script>
九:Go
resp, err := http.Get("http://example.com/")
...
resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
...
resp, err := http.PostForm("http://example.com/form",
url.Values{"key": {"Value"}, "id": {"123"}})
代码说明:https://golang.org/pkg/net/http/
【Azure 环境】各种语言版本或命令,发送HTTP/HTTPS的请求合集的更多相关文章
- Linux命令发送Http GET/POST请求
Get请求 curl命令模拟Get请求: 1.使用curl命令: curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到 ...
- PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)
源码地址:https://github.com/Tinywan/PHP_Experience 测试环境配置: 环境:Windows 7系统 .PHP7.0.Apache服务器 PHP框架:ThinkP ...
- 非域环境下搭建自动故障转移镜像无法将 ALTER DATABASE 命令发送到远程服务器实例的解决办法
非域环境下搭建自动故障转移镜像无法将 ALTER DATABASE 命令发送到远程服务器实例的解决办法 环境:非域环境 因为是自动故障转移,需要加入见证,事务安全模式是,强安全FULL模式 做到最后一 ...
- [Swift通天遁地]五、高级扩展-(13)图片资源本地化设置:根据不同的语言环境显示不同语言版本图片
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【Azure 环境】使用Microsoft Graph PS SDK 登录到中国区Azure, 命令Connect-MgGraph -Environment China xxxxxxxxx 遇见登录错误
问题描述 通过PowerShell 连接到Microsoft Graph 中国区Azure,一直出现AADSTS700016错误, 消息显示 the specific application was ...
- Qt 编写应用支持多语言版本--一个GUI应用示例
简介 上一篇博文已经说过如何编写支持多语言的Qt 命令行应用,这一篇说说Qt GUI 应用多语言支持的坑. 本人喜欢用代码来写布局,而不是用 Qt Designer 来设计布局,手写布局比 Qt De ...
- C#环境变量配置及csc命令详解(转自cy88310)
C#环境变量设置步骤: 在桌面右击[我的电脑]->[属性]->[高级]->[环境变量] 在下面的系统变量栏点击"新建" 变量名输入"csc" ...
- MAC下 JDK环境配置、版本切换以及ADB环境配置
网上方法,自己总结:亲测可行! 一.JDK环境配置.版本切换: 通过命令’jdk6′, ‘jdk7′,’jdk8’轻松切换到对应的Java版本: 1.首先安装所有的JDk:* Mac自带了的JDK6, ...
- Linux下查看内核、CPU、内存及各组件版本的命令和方法
Linux下查看内核.CPU.内存及各组件版本的命令和方法 Linux查看内核版本: uname -a more /etc/*release ...
- Azure环境中Nginx高可用性和部署架构设计
前几篇文章介绍了Nginx的应用.动态路由.配置.在实际生产环境部署时,我们需要同时考虑Nginx的高可用性和部署架构. Nginx自身不支持集群以保证自身的高可用性,商业版本的Nginx+推荐: T ...
随机推荐
- Linux下面rsync 实现 完全一致的同步方法
1. 在某些特殊的linux机器上面, 比如龙芯后者是飞腾服务器,部分工具不太好用, 需要使用x86弄好之后进行同步过去, 这个时候scp 最简单但是网络流量非常大, 不如使用rsync, rsync ...
- [转贴]英特尔Sapphire Rapids至强可扩展CPU完整型号爆料与路线图展望
2022-10-13 15:15· 稿源: cnbeta 腾讯云服务器促销:2核2G首年仅需40元 历史新低 @结城安穗-YuuKi_AnS 刚刚在社交媒体上,分享了与英特尔下一代 Sapph ...
- docker -- images镜像消失问题排查
1. 问题描叙 安装model-serving组件时,错误日志输出push时对应的tag不存在,导致镜像推送失败 2. 问题排查 # 找到对应镜像,尝试手动推送 docker images|grep ...
- 【K哥爬虫普法】百亿电商数据,直接盗取获利,被判 5 年!
我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K 哥特设了"K哥爬虫普法"专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识, ...
- 语言模型的预训练[6]:思维链(Chain-of-thought,CoT)定义原理详解、Zero-shot CoT、Few-shot CoT 以及在LLM上应用
大语言模型的预训练[6]:思维链(Chain-of-thought,CoT)定义原理详解.Zero-shot CoT.Few-shot CoT 以及在LLM上应用 1.思维链定义 背景 在 2017- ...
- C# 多线程与线程扫描器
多线程是一种复杂的编程技术,可以同时运行多个独立的线程来处理各种任务.在C#中,可以使用Thread类和ThreadPool类来实现多线程编程.Thread类用于创建和控制线程.可以使用Thread. ...
- Python 代码推送百度链接
通过代码实现抓取个人博客中某一页指定文章链接,并批量将该链接推送到百度站长平台,起到快速收录的目的. import sys import requests from bs4 import Beauti ...
- React的组件通信与状态管理
目录 1. 组件通讯-概念 1.组件的特点 2.知道组件通讯意义 总结: 2. 组件通讯-props 基本使用 1.传递数据和接收数据的过程 2.函数组件使用 props 3.类组件使用 props ...
- cs50ai3
cs50ai3-------Optimization cs50ai3-------Optimization 基础知识 课后题目 代码实践 学习链接 总结 基础知识 这节课主要讲了一些优化问题对应的算法 ...
- CF1764H Doremy's Paint 2 题解
题目链接:CF 或者 洛谷 高分题,感觉挺有意思的题,值得一提的是这个题的 \(1\) 和 \(3\) 版本却是两个基础题. 一开始以为跟这道差不多:P8512 [Ynoi Easy Round 20 ...