C#,Delphi,Oracle,MSSQL 개발자블로그
HTTPREQUEST/HTTPRESPONSE (Open API 호출 시 사용) 본문
밑에 소스는 Google Oauth 2.0 인증과 관련해서 RefeshToken 값을 이용해 AccessToken 값을 구하는 구글 API입니다.
"####################################" 이 들어간 문자는 중요한 값이라 일부러 숨겼습니다.
"대충 이런식으로 쓴다" 정도로 봐주시면 감사하겠습니다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace Http_Test { class Program { static void Main( string [] args) { // 요청을 보내는 URI string strUri = "https://accounts.google.com/o/oauth2/token" ; // POST, GET 보낼 데이터 입력 StringBuilder dataParams = new StringBuilder(); dataParams.Append( "client_id=#############################################" ); dataParams.Append( "&client_secret=####################################" ); dataParams.Append( "&refresh_token=####################################" ); dataParams.Append( "&grant_type=refresh_token" ); // 요청 String -> 요청 Byte 변환 byte [] byteDataParams = UTF8Encoding.UTF8.GetBytes(dataParams.ToString()); ///////////////////////////////////////////////////////////////////////////////////// /* POST */ // HttpWebRequest 객체 생성, 설정 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUri); request.Method = "POST" ; // 기본값 "GET" request.ContentType = "application/x-www-form-urlencoded" ; request.ContentLength = byteDataParams.Length; /* GET */ // GET 방식은 Uri 뒤에 보낼 데이터를 입력하시면 됩니다. /* HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUri + "?" + dataParams); request.Method = "GET"; */ ////////////////////////////////////////////////////////////////////////////////////// // 요청 Byte -> 요청 Stream 변환 Stream stDataParams = request.GetRequestStream(); stDataParams.Write(byteDataParams, 0, byteDataParams.Length); stDataParams.Close(); // 요청, 응답 받기 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // 응답 Stream 읽기 Stream stReadData = response.GetResponseStream(); StreamReader srReadData = new StreamReader(stReadData, Encoding.Default); // 응답 Stream -> 응답 String 변환 string strResult = srReadData.ReadToEnd(); Console.WriteLine(strResult); Console.ReadLine(); } } } |
출처: http://j07051.tistory.com/556 [흘러간다...]
'Programming > C#' 카테고리의 다른 글
WinForm과 WPF 2 (0) | 2018.07.12 |
---|---|
WinForm과 WPF (0) | 2018.07.12 |
C# 관리자 권한으로 빌드. (0) | 2017.11.06 |
C# 마우스와 키보드 (0) | 2017.08.10 |
[C#] 콤보박스의 SelectedIndex가 작동하지 않을 때 해결방법. (0) | 2017.08.10 |
Comments