Calling a webservice repeatedly and export data - json

I have a webservice which accepts a parameter and gives back result in a JSON message format.
http://portLocation/Company/WebService.asmx/Operation?parameter1=XX
My objective is to repeatedly call this webservice with the parameters in a CSV File.
What would be the ideal way to do it.

Here is a simple working concept for C#.
You'll need to adjust it for your needs.
static void Main(string[] args)
{
string datafile = "data.csv";
StringBuilder sbUrl = new StringBuilder("http://portLocation/Company/WebService.asmx/Operation");
List<string> fileContents = System.IO.File.ReadAllLines(datafile).ToList();
using (HttpClient httpClient = new HttpClient())
{
foreach (string item in fileContents)
{
string[] data = item.Split(',');
sbUrl.AppendFormat("?parameter1={0}", data[0]);
sbUrl.AppendFormat("&parameter2={0}", data[1]);
sbUrl.AppendFormat("&parameter3={0}", data[2]);
sbUrl.AppendFormat("&parameter4={0}", data[3]);
var response = httpClient.GetAsync(sbUrl.ToString());
}
}
}

Related

How to stream large json object to browser in ASP.Net Core

When stream large json object to browser, to prevent OutOfMemoryException; in .Net Framework, I was able to use
public static void StreamJsonThenEnd(this HttpResponseBase httpResponse, object data)
{
httpResponse.ContentType = "application/json";
using (StreamWriter writer = new StreamWriter(httpResponse.OutputStream))
using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer ser = new JsonSerializer();
ser.Serialize(jsonWriter, data);
jsonWriter.Flush();
}
httpResponse.End();
}
But when I worked on .Net Core, I tried below code but it gave me Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead
public static void StreamJsonThenEnd(this HttpResponse httpResponse, object data)
{
httpResponse.ContentType = "application/json";
httpResponse.StatusCode = StatusCodes.Status200OK;
using (StreamWriter writer = new StreamWriter(httpResponse.Body))
using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer ser = new JsonSerializer();
ser.Serialize(jsonWriter, data);
jsonWriter.Flush();
}
}
I also tried
public static async Task StreamJsonThenEnd(this HttpResponse httpResponse, object data)
{
httpResponse.ContentType = "application/json";
httpResponse.StatusCode = StatusCodes.Status200OK;
await System.Text.Json.JsonSerializer.SerializeAsync(httpResponse.Body, data, data.GetType());
}
but it now throw The response headers cannot be modified because the response has already started
How can I stream my json object in ASP.Net Core?
Look like Json is not actually root cause of my problem, as in .Net Core it already used stream for json by default JsonResultExecutor

what is the best way to post http request from ssis package

i want to call a http post request through my ssis package. But not sure what should be the best way of calling a rest api post method. Please help .
You can use this method within a Script Task to make the api call using httpclient:
public void Post(Produto produto)
{
var client = new HttpClient;
client.BaseAddress = new Uri("https://localhost:5001/api/");
client.DefaultRequestHeaders.Clear();
//client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.Add("ApiKey", "");
var jsonBody = JsonConvert.SerializeObject(produto);
client.PostAsync("createproduto", new StringContent(jsonBody, Encoding.UTF8, "application/json"));
}
You can make use of the namespace System.Net.WebClient to make the Http request with the help of Script Task in SSIS. Following example shows how this can be achieved. The example was created in SSIS 2008 R2.
public void Main()
{
Variables varCollection = null;
Dts.VariableDispenser.LockForRead("User::RemoteUri");
Dts.VariableDispenser.LockForRead("User::LocalFolder");
Dts.VariableDispenser.GetVariables(ref varCollection);
System.Net.WebClient myWebClient = new System.Net.WebClient();
string webResource = varCollection["User::RemoteUri"].Value.ToString();
string fileName = varCollection["User::LocalFolder"].Value.ToString() + webResource.Substring(webResource.LastIndexOf('/') + 1);
byte[] data;
using (WebClient client = new WebClient())
{
data = client.DownloadData(webResource);
}
FileInfo file = new System.IO.FileInfo(fileName);
file.Directory.Create(); // If the directory already exists, this method does nothing.
File.WriteAllBytes(file.FullName, data);
Dts.TaskResult = (int)ScriptResults.Success;
}

get Json Data From URL in C#

I am new to json i want get json data from a link, here from web search i have written code
private void button1_Click(object sender, EventArgs e)
{
string url = #"http://hololens5.northeurope.cloudapp.azure.com/INTERSHOP/web/WFS/inSPIRED-inTRONICS_Business-Site/en_US/-/USD/ViewProduct-Start?SKU=1599925&CategoryName=151&CatalogID=Computers";
using (WebClient wc=new WebClient())
{
json = wc.DownloadString(url);
}
string path = #"ouptputfileJSON.json";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(json);
}
}
}
When i execute this code i'm getting output in html page. how to get in json data of select product in the link provided
Here is the rest endpoint you are looking for:
http://hololens5.northeurope.cloudapp.azure.com/INTERSHOP/rest/WFS/inSPIRED-
inTRONICS_Business-Site/-;loc=en_US;cur=USD/products/1599925
Documentation about other rest endpoints:
https://support.intershop.com/kb/index.php/Display/T27711
Because
http://hololens5.northeurope.cloudapp.azure.com/INTERSHOP/web/WFS/inSPIRED-inTRONICS_Business-Site/en_US/-/USD/ViewProduct-Start?SKU=1599925&CategoryName=151&CatalogID=Computers
Is webpage and not API endpoint so you need to find proper endpoint from where you want to get data
Once you get Proper Endpoint you can use below
Here is an example how you can use httpclient to make a request
static void Main()
{
Task t = new Task(DownloadPageAsync);
t.Start();
Console.WriteLine("Downloading page...");
Console.ReadLine();
}
static async void DownloadPageAsync()
{
// ... Endpoint
string page = "request URL";
// ... Use HttpClient.
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(page))
using (HttpContent content = response.Content)
{
// ... Read the string.
string result = await content.ReadAsStringAsync();
Console.WriteLine(result);
}
}

JSON.NET: getting json from external source with streams, how to get just one value?

I have the following code:
static void Main(string[] args)
{
HttpClient client = new HttpClient();
using (Stream stream = client.GetStreamAsync("https://opendata.rdw.nl/resource/8ys7-d773.json?kenteken=61SFSL").Result)
using (StreamReader streamReader = new StreamReader(stream))
using (JsonReader reader = new JsonTextReader(streamReader))
{
JsonSerializer serializer = new JsonSerializer();
// read the json from a stream
// json size doesn't matter because only a small piece is read at a time from the HTTP request
//What do I do here to get my one value?
}
Console.WriteLine("Press any key to continue...");
Console.Read();
}
I got this from the documentation over at the JSON.NET website. The reason being that I don't want to load the whole string, but piece by piece. The response is as follows:
[{"brandstof_omschrijving":"Benzine","brandstof_volgnummer":"1","brandstofverbruik_buiten":"6.60","brandstofverbruik_gecombineerd":"8.20","brandstofverbruik_stad":"11.10","co2_uitstoot_gecombineerd":"196","emissiecode_omschrijving":"Euro 4","geluidsniveau_rijdend":"71","geluidsniveau_stationair":"82","kenteken":"61SFSL","milieuklasse_eg_goedkeuring_licht":"70/220*2001/100B","nettomaximumvermogen":"99.00","toerental_geluidsniveau":"4125"}]
I.e., it returns an array with one json object, and I want to retrieve just one value in there, using a stream. How might I do this?
You could try the following
using System;
using System.Net.Http;
using Newtonsoft;
public class Program {
public static void Main() {
var client = new HttpClient();
var json = client.GetStringAsync("https://opendata.rdw.nl/resource/8ys7-d773.json?kenteken=61SFSL").Result;
var data = JsonConvert.DeserializeObject<dynamic>(json);
string value = data[0].co2_uitstoot_gecombineerd;
Console.WriteLine(value);
Console.WriteLine("Press any key to continue...");
Console.Read();
}
}

How to write the WCF Service program which connect to database and get the data in json format

I m new to Web search. I dont know how to write the program in web service please help me out
in program i want to connect the web service to database then from there i am getting the data in json format
in client side i m using jquery mobile framework,jquery Ajax
suppose in database
id title
1 asd
2 asw
Here is an example which I have copied from some of my code.
WCF Interface definition
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
[ServiceContract]
public interface IGraphDataProvider
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "devices")]
List<string> get_devices();
}
WCF Implementation
public class GraphDataProvider : IGraphDataProvider
{
/**
* #brief Return a (possibly empty) list of devices listed in the configuration DB
* */
public List<string> get_devices()
{
// If you want to modify your headers...
// WebOperationContext.Current.OutgoingResponse.Headers["Access-Control-Allow-Origin"] = "*";
// Now just return a list of strings, WCF will convert to JSON
return getDevices();
}
}
That takes care of the JSON response. In case you don't know how to read your SQL DB, there are a couple ways.
You could use Entity Framework. It's easy and convenient, once you have it set up your code will look like:
public static List<string> getDevices()
{
var db_context= new CfgEntities();
var devices = from row in db_context.Devices
where !row.Device.StartsWith("#")
select row.Device.Trim();
return devices.Distinct().ToList();
}
Use the SQL Client from Microsoft. Your code will look like this:
using System.Data.SqlClient;
// ...
public static List<string> getDevices()
{
var sql_connection_ = new SqlConnection();
sql_connection_.ConnectionString = string.Format("Server=localhost; database={0}; Trusted_Connection=SSPI", dbName);
try
{
sql_connection_.Open();
}
// catch exceptions etc. If Open() worked then you have a connection.
string queryString = "SELECT [Device] from [Config].[dbo].[Devices]";
// Now I'm just copying shamelessly from MSDN...
SqlCommand command = new SqlCommand(queryString, sql_connection_);
SqlDataReader reader = command.ExecuteReader();
List<string> return_list = new List<string>();
while (reader.Read())
{
return_list.Add((string)reader[0]);
}
return return_list;
}