c#发送请求访问外部接口

 string url = "https://cloud.soei.com.cn/smsapi/sms/verifycode";
            HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(url);
            //表头参数
            string token = "9c0025b4aae442bda5498971ec1ab219";
            httpClient.DefaultRequestHeaders.Add("token", token);
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                using (var request = new HttpRequestMessage())
                {
                    var postBody = $"{{\"identity\":\"{identity}\",\"phoneNumber\":\"{phoneNumber}\",\"code\":\"{code}\"}}";
                    request.Method = HttpMethod.Post;
                    request.Content = new StringContent(postBody, Encoding.UTF8, "application/json");
                    var response = await httpClient.SendAsync(request);
           

你可能感兴趣的:(c#开发)