728x90
ASP.Net 에서의 웹서비스 get / post에 대한 예제
using System;
using System.Diagnostics;
using System.Web;
public class ProcessHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// Get the method used to call the page (GET or POST)
string method = context.Request.HttpMethod;
if (method == "GET") {
// Get the parameters from the query string
string param = context.Request.QueryString["param"];
// Execute the Notepad.exe process with the parameter
ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe", param);
Process.Start(startInfo);
// Send a response back to the client
context.Response.ContentType = "text/plain";
context.Response.Write("Notepad.exe process started with parameter: " + param);
} else if (method == "POST") {
// Get the parameters from the request body
string param = context.Request.Form["param"];
// Execute the Notepad.exe process with the parameter
ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe", param);
Process.Start(startInfo);
// Send a response back to the client
context.Response.ContentType = "text/plain";
context.Response.Write("Notepad.exe process started with parameter: " + param);
} else {
// Invalid request method
context.Response.StatusCode = 405;
context.Response.Write("Invalid request method.");
}
}
public bool IsReusable {
get {
return false;
}
}
}
Http Get method의 파라미터로 받은 url에서 파일을 받는다
받은 파일은 base64로 인코딩 된 파일이고 이를
byte로 디코딩하여 다운로드 한다.
using System;
using System.Net;
using System.IO;
using System.Web;
class Program {
static void Main(string[] args) {
string urlParam = "url"; // The name of the URL parameter in the query string
string base64Data = ""; // The variable to store the base64 encoded file data
try {
// Get the URL parameter from the query string
string url = HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString[urlParam]);
// Create a WebClient object to make the GET request
using (WebClient client = new WebClient()) {
// Make the GET request and get the response data as a byte array
byte[] fileData = client.DownloadData(url);
// Convert the byte array to a base64 string
base64Data = Convert.ToBase64String(fileData);
}
// Decode the base64 string to a byte array
byte[] decodedData = Convert.FromBase64String(base64Data);
// Save the decoded data to a file
using (FileStream stream = new FileStream("file.pdf", FileMode.CreateNew)) {
stream.Write(decodedData, 0, decodedData.Length);
}
Console.WriteLine("File downloaded successfully.");
} catch (WebException ex) {
Console.WriteLine("Error downloading file: " + ex.Message);
} catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
728x90
728x90