restful(http)请求 Get方法

请求特定地址,将地址上的内容拿下来
string serviceAddress = @"https://api.xxxxx.com/search/repositories?q=language:csharp&sort=stars";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
            request.Method = "GET";
            request.UserAgent = "Code Sample Web Client";
            //凭证 身份信息(一般请求不加的话,最常见的错误是403)
            request.Credentials = CredentialCache.DefaultCredentials;

            //request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
               // string str =  myStreamReader.ReadToEnd();
				//获取特定的对象数据
            analysisDTO authResult = JsonConvert.DeserializeObject<analysisDTO>(myStreamReader.ReadToEnd());
            
            string str = JsonConvert.SerializeObject(authResult);
            //打印出来
      			Console.WriteLine(str);

将特定网址上的内容保存到指定的路径文件上

       //检查是否有这个文件,文件中是否有内容,有的话进行覆盖   (str)是字符串内容
        System.IO.File.WriteAllText(@"C:\Users\Desktop\Test.txt(这里放的是指定路径)", str);
        //StreamWriter类,以流的方式写入
        System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Desktop\Test.txt(得到的内容进行写入)", true);

你可能感兴趣的:(rest,http,后端,RestFul,HTTP请求,特定地址请求)