178 lines
6.2 KiB
C#
178 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.UI;
|
|
using System.Web.UI.WebControls;
|
|
using System;
|
|
using System.Configuration;
|
|
using System.Data.SqlClient;
|
|
using SecuringWebApiUsingApiKey.Middleware;
|
|
using Model;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
|
|
|
|
namespace OnDocAPI_NetFramework
|
|
{
|
|
public partial class dt : System.Web.UI.Page
|
|
{
|
|
string _connectionString = StringCipher.Decrypt(ConfigurationManager.ConnectionStrings["DocTesterconnectionstring"].ConnectionString, "i%!k!7pab%bNLdA5hE4pkR4XaB%E^jB3d9tHuQ4pbF&BZjF7SB#WBWit5#HrbJiLrLVm");
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void ResetPdf()
|
|
{
|
|
pdfFrame.Attributes["src"] = "about:blank";
|
|
}
|
|
protected void btnLoad_Click(object sender, EventArgs e)
|
|
{
|
|
ResetPdf();
|
|
string json = "";
|
|
using (var con = new SqlConnection(_connectionString))
|
|
using (SqlCommand cmd = new SqlCommand(
|
|
"SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
|
|
{
|
|
cmd.Parameters.AddWithValue("@key", txtKey.Text);
|
|
con.Open();
|
|
json = cmd.ExecuteScalar()?.ToString();
|
|
}
|
|
|
|
if (json == null)
|
|
{
|
|
using (SqlConnection con = new SqlConnection(_connectionString))
|
|
using (SqlCommand cmd = new SqlCommand(
|
|
"SELECT JsonObjekt FROM _OnDoc_API_TestScripts WHERE id = @key", con))
|
|
{
|
|
cmd.Parameters.AddWithValue("@key", txtKey.Text);
|
|
con.Open();
|
|
json = cmd.ExecuteScalar()?.ToString();
|
|
}
|
|
|
|
}
|
|
hfJson.Value = json;
|
|
}
|
|
|
|
protected void btnSave_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
using (SqlConnection con = new SqlConnection(_connectionString))
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
IF EXISTS (SELECT 1 FROM _OnDoc_API_TestScripts WHERE id = @key)
|
|
UPDATE _OnDoc_API_TestScripts SET JsonObjekt =@json
|
|
WHERE id = @key
|
|
ELSE
|
|
INSERT INTO _OnDoc_API_TestScripts (id,JsonObjekt)
|
|
VALUES (@key, @json)
|
|
", con))
|
|
|
|
//IF EXISTS (SELECT 1 FROM provdokuments WHERE provdokumentid = @key)
|
|
// UPDATE provdokuments SET JavaScriptObject = @json, geaendertam = GETDATE()
|
|
// WHERE provdokumentid = @key
|
|
//ELSE
|
|
// INSERT INTO provdokuments (provdokumentid,erstelltam,geaendertam, JavaScriptObject)
|
|
// VALUES (@key, getdate(),getdate(),@json)
|
|
//", con))
|
|
{
|
|
cmd.Parameters.AddWithValue("@key", txtKey.Text);
|
|
cmd.Parameters.AddWithValue("@json", hfJson.Value);
|
|
con.Open();
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
protected void btnPdf_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
//byte[] pdf = PdfGenerator.CreatePdfFromJson(hfJson.Value);
|
|
|
|
//string base64 = Convert.ToBase64String(pdf);
|
|
//pdfFrame.Attributes["src"] =
|
|
// "data:application/pdf;base64," + base64;
|
|
try
|
|
{
|
|
APIErrorSimple apireturn = new APIErrorSimple();
|
|
APIOK apiok = new APIOK();
|
|
;
|
|
string jsonstring = hfJson.Value;
|
|
|
|
WebRequest request;
|
|
|
|
var data = Encoding.UTF8.GetBytes(jsonstring);
|
|
string OwnHost = System.Configuration.ConfigurationManager.AppSettings["OwnHost"].ToString();
|
|
string uri = OwnHost + "/API/CreateCLM";
|
|
uri = OwnHost + "/API/DokumentGenerator";
|
|
request = WebRequest.Create(uri);
|
|
request.ContentLength = data.Length;
|
|
request.ContentType = "application/json";
|
|
request.Method = "POST";
|
|
request.Headers["Authorization"] = "Bearer " + System.Configuration.ConfigurationManager.AppSettings["OwnToken"].ToString();
|
|
using (Stream requestStream = request.GetRequestStream())
|
|
{
|
|
requestStream.Write(data, 0, data.Length);
|
|
requestStream.Close();
|
|
|
|
using (Stream responseStream = request.GetResponse().GetResponseStream())
|
|
{
|
|
using (var reader = new StreamReader(responseStream))
|
|
{
|
|
var response = reader.ReadToEnd();
|
|
try
|
|
{
|
|
|
|
|
|
apiok = JsonConvert.DeserializeObject<APIOK>(response);
|
|
var jo = JObject.Parse(response.ToString());
|
|
byte[] pdf = null;
|
|
pdfFrame.Attributes["src"] =
|
|
"data:application/pdf;base64," + apiok.file;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
apireturn = JsonConvert.DeserializeObject<APIErrorSimple>(response);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
catch
|
|
{
|
|
byte[] pdf;
|
|
|
|
}
|
|
|
|
|
|
// var request = (HttpWebRequest)WebRequest.Create("https://api.example.com/generate-pdf");
|
|
//request.Method = "POST";
|
|
//request.ContentType = "application/json";
|
|
|
|
//byte[] payload = Encoding.UTF8.GetBytes(json);
|
|
//request.ContentLength = payload.Length;
|
|
|
|
//using (var stream = request.GetRequestStream())
|
|
// stream.Write(payload, 0, payload.Length);
|
|
|
|
//using (var response = (HttpWebResponse)request.GetResponse())
|
|
//using (var rs = response.GetResponseStream())
|
|
//using (var ms = new MemoryStream())
|
|
//{
|
|
// rs.CopyTo(ms);
|
|
// return ms.ToArray();
|
|
//}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|