TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Steve Henry
NA
20
3.5k
Using Json with authentification and get the result
Oct 4 2017 10:41 PM
Hy, I'm new in Json, I just wanna try to send the data and get the response from Json
here's the data
{
"CardCode": "C001",
"PostingDate": "2017-05-17",
"DocDueDate": "2017-05-17",
"TaxDate": "2017-05-17",
"SalesPersonCode": "-1",
"DocumentNumberingPOS":"2",
"PrimaryNumberingPOS":"A002",
"lines": [{
"ItemCode": "ITEM0001",
"WarehouseCode": "PST-000",
"Quantity": "1",
"Price": "50000"
}, {
"ItemCode": "ITEM0002",
"WarehouseCode": "PST-000",
"Quantity": "1",
"Price": "10000"
}
]
}
and this is the result
{
"errorCode": "0",
"message": "Data has beed added",
"value": "22"
}
and this is my code
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; }
}
Does anyone could help me to fix this code
thank you
regards,
steve Henry
Reply
Answers (
2
)
csv file to pdf file convertor c#
No value given for one or more required parameters error C#