본문 바로가기
Programming

https GET 결과 가져오기

by Mizix 2011. 10. 6.
반응형
기본 HTTP 호출

WebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();

string tempString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?

C#에서 인증서 까지 가져오기.

public string GetHttpResult(string urlStr)
{
string certPath = cqLog.AppPath + "\\" + tsi.Host + ".cer";
if (!File.Exists(certPath))
{
// 오류
}
X509Certificate cert = X509Certificate.CreateFromCertFile(certPath);
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
request.ClientCertificates.Add(cert); // Attaching the Certificate To the request
TrustAllCert ValCallback = new TrustAllCert();
ServicePointManager.ServerCertificateValidationCallback =
new System.Net.Security.RemoteCertificateValidationCallback(ValCallback.OnValidationCallback);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();

string tempString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?

// print out page source
//Console.WriteLine(sb.ToString());
return sb.ToString();
}

...

public class TrustAllCert
{
public TrustAllCert()
{
}

public bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}

반응형