using System;
using System.IO;
using System.Net;
using System.Text;
public String ConvertImageURLToBase64(String ID)
{

            string url = "https://.../pictures/" + ID + "?key="special api key"";
            //create an object of StringBuilder type.
            StringBuilder _sb = new StringBuilder();
            //create a byte array that will hold the return value of the getImg method
            Byte[] _byte = this.GetImg(url);
            //appends the argument to the stringbulilder object (_sb)
            _sb.Append(Convert.ToBase64String(_byte, 0, _byte.Length));
            //return the complete and final url in a base64 format.
            return string.Format(@"data:image/jpg;base64, {0}", _sb.ToString());
            
}
private byte[] GetImg(string url)
{
            //create a stream object and initialize it to null
            Stream stream = null;
            //create a byte[] object. It serves as a buffer.
            byte[] buf;
            try
            {
                //Create a new WebProxy object.
                WebProxy myProxy = new WebProxy();
                //create a HttpWebRequest object and initialize it by passing the colleague api url to a create method.
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                //Create a HttpWebResponse object and initilize it
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                //get the response stream
                stream = response.GetResponseStream();

                using (BinaryReader br = new BinaryReader(stream))
                {
                    //get the content length in integer
                    int len = (int)(response.ContentLength);
                    //Read bytes
                    buf = br.ReadBytes(len);
                    //close the binary reader
                    br.Close();
                }
                //close the stream object
                stream.Close();
                //close the response object 
                response.Close();
            }
            catch (Exception exp)
            {
                //set the buffer to null
                buf = null;
            }
            //return the buffer
            return (buf);
}

'공부방 > ASP.NET' 카테고리의 다른 글

IIS URL rewrite 모듈 설치 및 이용방법  (0) 2020.08.24

1. URL Rewrite(URL 재작성) 설치

URL 재작성을 사용하기 위해서는 웹 플랫폼 인스톨러를 통해서 URL 재작성 기능을 설치해야 합니다.
다운로드 : 
Web Platform Installer

웹 플랫폼 인스톨러에서 URL 재작성 기능을 검색

URL 재작성 2.0 추가 -> 설치(I) 진행

IIS 관리자에서 웹사이트를 선택 IIS기능 목록에 'URL 재작성' 메뉴가 있으면 설치 성공

 

2. 이용 방법

추가 1. ASP.NET_SessionId 쿠키 SameSite=None; Secure 옵션 지정
추가 2. HTTPS 리다이렉트 rule

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.webServer>
    	  <!--Chrome 80업데이트 Session Cookie SameSite=None; Secure 옵션 이슈로 rewrite-'AddSameSiteCookieHttps' rule 추가 1-->
	      <rewrite> <!--VS2010에서는 잘못된 자식요소로 인식--> 
	        <outboundRules> 
	          <rule name="AddSameSiteCookieHttps" preCondition="Https" enabled="true" stopProcessing="true"> 
	            <match serverVariable="RESPONSE_Set-Cookie" pattern="^(.*)(ASP\.NET_SessionId)(=)(.*)$" /> 
	            <action type="Rewrite" value="{R:0}; SameSite=None; Secure" /> 
	          </rule> 
	          <preConditions> 
	            <preCondition name="Https"> 
	              <add input="{HTTPS}" pattern="^ON$" /> 
	              <add input="{HTTP_USER_AGENT}" pattern="iphone|ipad|ipod" negate="true" /> 
	            </preCondition> 
	          </preConditions> 
	        </outboundRules>
            <!--Session Cookie Secure 옵션 이슈로 'RedirectHTTPtoHTTPS' rule 추가 2-->
	        <rules> 
	          <rule name="RedirectHTTPtoHTTPS" stopProcessing="true"> 
	            <match url="(.*)"/> 
	            <conditions> 
	              <add input="{HTTPS}" pattern="^OFF$"/> 
	            </conditions> 
	            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/> 
	          </rule> 
	        </rules> 
	      </rewrite>
	</system.webServer>
</configuration>



 

'공부방 > ASP.NET' 카테고리의 다른 글

Convert Image URL to base 64 function  (0) 2020.09.09

+ Recent posts