50٪ تخفیف روی تمام دوره‌ها!
پایان تخفیف تا:
مشاهده دوره‌ها
0

استفاده از Api یک سایت

سلام خسته نباشید

می خواستم بدونم در سی شارپ چگونه میشه به یک api دسترسی پیدا کرد

برای استفاده درون ویندوز فرم می خواهم

اطلاعات api را ضمیمه کردم

ممنون می شوم راهنمایی ام کنید

پرسیده شده در 1398/02/30 توسط

4 پاسخ

0

سلام به شما دوست عزیز.

خب شما فقط نیاز دارید که از متد post استفاده کنید و بخش آدرس رو به شکلی که در کد php گفته شده توی کد سی شارپ تغییر بدین و مشکل حل خواهد شد.

پاسخ در 1398/03/07 توسط
0

سلام به شما دوست عزیز.

ضمیمه شما قابل دریافت و مشاهده نیست اگر ممکنه از یه راه دیگه ضمیمه رو نشون بدین ولی راه کلی برای ارتباط با api ها در سی شارپ به روش زیر هستش دقت کنید که کد زیر هر 4 متد اصلی api ها یعنی put,post, get, delete رو اورده

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace HttpClientSample
{
    public class Product
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public string Category { get; set; }
    }

    class Program
    {
        static HttpClient client = new HttpClient();

        static void ShowProduct(Product product)
        {
            Console.WriteLine($"Name: {product.Name}\tPrice: " +
                $"{product.Price}\tCategory: {product.Category}");
        }

        static async Task<Uri> CreateProductAsync(Product product)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(
                "api/products", product);
            response.EnsureSuccessStatusCode();

            // return URI of the created resource.
            return response.Headers.Location;
        }

        static async Task<Product> GetProductAsync(string path)
        {
            Product product = null;
            HttpResponseMessage response = await client.GetAsync(path);
            if (response.IsSuccessStatusCode)
            {
                product = await response.Content.ReadAsAsync<Product>();
            }
            return product;
        }

        static async Task<Product> UpdateProductAsync(Product product)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(
                $"api/products/{product.Id}", product);
            response.EnsureSuccessStatusCode();

            // Deserialize the updated product from the response body.
            product = await response.Content.ReadAsAsync<Product>();
            return product;
        }

        static async Task<HttpStatusCode> DeleteProductAsync(string id)
        {
            HttpResponseMessage response = await client.DeleteAsync(
                $"api/products/{id}");
            return response.StatusCode;
        }

        static void Main()
        {
            RunAsync().GetAwaiter().GetResult();
        }

        static async Task RunAsync()
        {
            // Update port # in the following line.
            client.BaseAddress = new Uri("http://localhost:64195/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            try
            {
                // Create a new product
                Product product = new Product
                {
                    Name = "Gizmo",
                    Price = 100,
                    Category = "Widgets"
                };

                var url = await CreateProductAsync(product);
                Console.WriteLine($"Created at {url}");

                // Get the product
                product = await GetProductAsync(url.PathAndQuery);
                ShowProduct(product);

                // Update the product
                Console.WriteLine("Updating price...");
                product.Price = 80;
                await UpdateProductAsync(product);

                // Get the updated product
                product = await GetProductAsync(url.PathAndQuery);
                ShowProduct(product);

                // Delete the product
                var statusCode = await DeleteProductAsync(product.Id);
                Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }
    }
}
پاسخ در 1398/02/31 توسط
0
$url = 'https://arzoonlike.ir/api/v2';
	$ch = curl_init($url);	
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,
				'key="YOURKEY"&action="ACTION"&service="SERVICE NUMBER"&link="YOUR LINK"&amount="COUNT"');
					
		curl_setopt($ch, CURLOPT_POST, 1);
	 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	curl_close($ch); 
	

این یک نمونه php هست که به من دادن

پاسخ در 1398/03/06 توسط

پاسخ شما