C# 调用API接口,有Json传参

1、调用API有Json传参

        public static int GetHttpTest(string sendData, out string reslut)
        {
            reslut = "";
            try
            {
                byte[] data = System.Text.Encoding.UTF8.GetBytes(sendData); // Json传参
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create("https://.../api/TransferInfo");  // 制备web请求
                wbRequest.Proxy = null;     //现场测试注释掉也可以上传
                wbRequest.Method = "GET"; //请求类型
                wbRequest.ContentType = "application/json"; //参数类型 
                wbRequest.ContentLength = data.Length; //参数长度
                //wbRequest.Headers["ApiVersion"] = "5";

                //注入参数
                using (Stream wStream = wbRequest.GetRequestStream())         //using(){}作为语句,用于定义一个范围,在此范围的末尾将释放对象。
                {
                    wStream.Write(data, 0, data.Length);
                }

                //获取响应
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream, Encoding.UTF8))      //using(){}作为语句,用于定义一个范围,在此范围的末尾将释放对象。
                    {
                        reslut = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                reslut = e.Message;     //输出捕获到的异常,用OUT关键字输出
                return -1;              //出现异常,函数的返回值为-1
            }
            return 0;
        }

2、如果没有Json传参,参数可以直接拼接到url里面

public static int GetHttp(string Params, out string reslut)
        {
            reslut = "";
            try
            {               
                 HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create("https://...../api/TransferInfo?IPN="+IPN);  // 制备web请求
                wbRequest.Proxy = null;     //现场测试注释掉也可以上传
                wbRequest.Method = "GET";
                wbRequest.Headers["ApiVersion"] = "5";
                
                //获取响应
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream, Encoding.UTF8))      //using(){}作为语句,用于定义一个范围,在此范围的末尾将释放对象。
                    {
                        reslut = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                reslut = e.Message;     //输出捕获到的异常,用OUT关键字输出
                return -1;              //出现异常,函数的返回值为-1
            }
            return 0;
        }
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用 AWS API 接口并使用 AWS 鉴权方式,可以通过 AWS SDK for .NET 来实现。以下是一个简单的示例: 1. 首先,需要安装 AWS SDK for .NET。可以通过 NuGet 包管理器来安装。 2. 在代码中引入 AWS SDK 的命名空间: ```csharp using Amazon; using Amazon.Runtime; using Amazon.Runtime.CredentialManagement; using Amazon.S3; using Amazon.S3.Model; ``` 3. 创建 AWS 访问凭证对象。可以使用以下代码从 AWS 访问密钥文件或凭证配置文件中读取访问密钥和秘密访问密钥: ```csharp var options = new CredentialProfileOptions { AccessKey = "YOUR_ACCESS_KEY", SecretKey = "YOUR_SECRET_KEY" }; var profile = new CredentialProfile("profile-name", options); var profileAWSCredentials = new CredentialProfileStoreChain().TryGetAWSCredentials(profile, out var awsCredentials); ``` 4. 创建 AWS S3 客户端对象,并使用上一步中创建的访问凭证对象进行身份验证: ```csharp var region = RegionEndpoint.GetBySystemName("us-west-2"); // 指定 AWS 区域 var s3Client = new AmazonS3Client(awsCredentials, region); ``` 5. 调用 AWS S3 API 接口。以下是一个示例: ```csharp var request = new PutObjectRequest { BucketName = "my-bucket", Key = "my-object", ContentBody = "Hello World!" }; var response = await s3Client.PutObjectAsync(request); ``` 以上代码演示了如何使用 AWS SDK for .NET 调用 AWS S3 API 接口并使用 AWS 鉴权方式进行身份验证。根据实际情况,需要将代码中的访问密钥和秘密访问密钥替换为自己的凭证信息,并将区域、桶名和对象键等参数替换为实际的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值