Hy, I'm new in Json, I just wanna try to send the data and get the response from Json
public partial class Form1 : Form
{
private static void PostJson(string url, template postParameters)
{
string postData = JsonConvert.SerializeObject(postParameters);
byte[] bytes = Encoding.UTF8.GetBytes(postData);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = bytes.Length;
httpWebRequest.ContentType = "application/json";
string autorization = "admin" + ":" + "admin";
byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(autorization);
autorization = Convert.ToBase64String(binaryAuthorization);
autorization = "Basic " + autorization;
httpWebRequest.Headers.Add("AUTHORIZATION", autorization);
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Count());
}
var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}", httpWebResponse.StatusCode);
throw new ApplicationException(message);
}
}
private static void Result(string url, template postParameters)
{
//string respon;
string postData = JsonConvert.SerializeObject(postParameters);
byte[] bytes = Encoding.UTF8.GetBytes(postData);
System.Net.WebRequest request = System.Net.HttpWebRequest.Create("http://xxxxxxxxxxx/api/CustomerSalesOrder"); // This only for your testing
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";
httpWebRequest.ContentLength = bytes.Length;
httpWebRequest.ContentType = "application/json";
string autorization = "admin" + ":" + "admin";//Login username:admin and password: admin
byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(autorization);
autorization = Convert.ToBase64String(binaryAuthorization);
autorization = "Basic " + autorization;
httpWebRequest.Headers.Add("AUTHORIZATION", autorization); //System.Net.WebRequest request = System.Net.HttpWebRequest.Create("(ServerLocal)/WSLokalRest/Peserta/peserta/" + txtNoKartu.Text); Get This from your Client (live)
try
{
System.Net.WebResponse response1 = request.GetResponse();
System.IO.Stream stream = response1.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
string contents = reader.ReadToEnd();
respon = contents;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PostJson("http://xxxxxxxxxx/api/CustomerSalesOrder", new template{ });
var template = new template
{
CardCode = "C001",
PostingDate = "2017-05-17",
DocDueDate = "2017-05-17",
TaxDate = "2017-05-17",
SalesPersonCode = "-1",
DocumentNumberingPOS = "2",
PrimaryNumberingPOS = "A002",
};
template.Lines.Add(new template_item
{
ItemCode = "ITEM0001",
WarehouseCode = "PST-000",
Quantity = "1",
Price = "50000",
});
template.Lines.Add(new template_item
{
ItemCode = "ITEM0002",
WarehouseCode = "PST-000",
Quantity = "1",
Price = "10000"
});
txtRespon.Text = respon;
}
}
public class template
{
public string CardCode { get; set; }
public string PostingDate { get; set; }
public string DocDueDate { get; set; }
public string TaxDate { get; set; }
public string SalesPersonCode { get; set; }
public string DocumentNumberingPOS { get; set; }
public string PrimaryNumberingPOS { get; set; }
public static List<template_item> Lines = new List<template_item>();
}
public class template_item
{
public string ItemCode { get; set; }
public string WarehouseCode { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
}