Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何发出GET RESTful请求_C# - Fatal编程技术网

C# 如何发出GET RESTful请求

C# 如何发出GET RESTful请求,c#,C#,我需要在url中包含curl-H‘Context-type:application/json’。我不知道该怎么做,服务器响应404,非常感谢您的帮助 private string RequestVehicleData() { string make = ""; string postcode = ""; string registration = (string)(Session["regNo"]);

我需要在url中包含curl-H‘Context-type:application/json’。我不知道该怎么做,服务器响应404,非常感谢您的帮助

private string RequestVehicleData()
        {
            string make = "";
            string postcode = "";

            string registration = (string)(Session["regNo"]);
            make = txtmake.Text;
            postcode = txtpostcode.Text;


            //Make Request
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://www.check-mot.service.gov.uk/api/v1/mot-history/{0}/{1}/", registration, make));
            httpWebRequest.ContentType = "application/json";                      
            httpWebRequest.Method = "GET";


            //Get Response
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                return result;
            }
        }

试试HttpClient。以下是从MSDN复制的示例:

可能重复的
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:9000/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // New code:
    HttpResponseMessage response = await client.GetAsync("api/products/1");
    if (response.IsSuccessStatusCode)
    {
        Product product = await response.Content.ReadAsAsync>Product>();
        Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
    }
}