Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm developing a small application that has to interact with a JSON/REST service.
What is the easiest option to interact with it in my c# application.
I don't need to have the best performances, since it's just a tools that will do some synchronization once a day, I'm more oriented toward the ease of use and the time of development.
(the service in question will be our local JIRA instance).
I think the best way by far is to use RestSharp. It's a free Nuget Package that you can reference. It's very easy to use and this is the example from their website:
var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource
// easily add HTTP Headers
request.AddHeader("header", "value");
// add files to upload (works with compatible verbs)
request.AddFile(path);
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
// or automatically deserialize result
// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;
// easy async support
client.ExecuteAsync(request, response => {
Console.WriteLine(response.Content);
});
// async with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {
Console.WriteLine(response.Data.Name);
});
// abort the request on demand
asyncHandle.Abort();
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Currently working with Unity, this might a super basic question, but here goes.
I need to call a URL from my app in C#. This is done for analytics purposes, and so I don't want to open a web browser or anything, just call the URL and that's it. I know about Application.OpenURL() to open the browser, but how do I achieve this without opening the browser ?
You may try like this:
var client = new WebClient();
var x = client.DownloadString("http://example.com");
or
HttpWebRequest request = WebRequest.Create("http://example.com") as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream stream = response.GetResponseStream();
Use the WebClient class in the System.Net namespace.
It's a high level implementation of an HTTP client which is really easy to use.
Has a method called .DownloadString() which does exactly what you want - calls a URL using HTTP GET and returns the response as a string.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
using c#.
I just want to clarify something... I normally work with WCF. Can I call rest apis exactly like I call WCF? Or do I use WebClient and parse the responseStream? If the rest api returns string formatted as JSON would I then somehow format this json in the responseStream?
I have spent sometime Googling but there seems to be different advice for it.
to be specific are there any standards for rest api clients? Is it just down to choice?
You should look into HttpClient (For making REST calls) and Json.NET (For serializing / deserializing your json):
A simple Get request:
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(uri);
//will throw an exception if not successful
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<SomeType>(content);
Note HttpClient is built with an asynchronous API which preferably should be used with async/await keywords
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Good evening,
I am new in c# and asp.net
I have created an MVC 4 WEB Application and I am using aspx as the view.
I am trying to call a remote web API and unfortunately I do not get it, in order to display the data on my web site.
I created one controller and inside the controller in the Index() method I wrote this code:
public class CallAPIController : Controller
{
//
// GET: /CallAPI/
public async Task<string> Index()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://remoteWEBAPI/");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/data").Result; // Blocking call!
string json = await response.Content.ReadAsStringAsync();
Debug.WriteLine("Content: " + json);
return json;
}
}
I am new to this technologies, I have tried many things and I have been struggling with this for the last 4-5 hours.I do not know how to solve this problem. Could you please help me? I do not think it should be very difficult for someone expert familiar with these...
Assuming your WebAPI accepts GET method and returning a JSON string.
WebClient client = new WebClient();
client.Headers["Accept"] = "application/json";
string returnedString = client.DownloadString(new Uri("http://yourwebapi.com/api/data"));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I am looking for a library or class in c# that can parse sip packets.
I need functions that will help me get the
Call-ID field from the packet, types of requests, and basically breakdown the sip packet to its fields.
Does anybody know something that can help me?
Thanks, ofek
This class from my sipsorcery project can do it for you.
Update: If you have a string that contains a full SIP packet you can parse the full thing by using:
var req = SIPSorcery.SIP.SIPRequest.ParseSIPRequest(reqStr);
var headers = req.Header;
var resp = SIPSorcery.SIP.SIPResponse.ParseSIPResponse(respStr);
var headers = resp.Header;
If you don't know whether the SIP packet is a request or a response you can use the SIPMessage class:
var mess = SIPSorcery.SIP.SIPMessage.ParseSIPMessage(messStr, null, null);
var headers = SIPSorcery.SIP.SIPHeader.ParseSIPHeaders(mess.SIPHeaders);
Update 2:
Given you're using pcap.net to capture the SIP packets you are probably ending up with a block of bytes rather than a string. You can use the SIPMessage class to parse the SIP packet from a UDP payload:
var mess = SIPSorcery.SIP.SIPMessage.ParseSIPMessage(packet.Ethernet.IPv4datagram.Udp.Payload, null, null);
var headers = SIPSorcery.SIP.SIPHeader.ParseSIPHeaders(mess.SIPHeaders);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I need to implement a method for checking plagiarism in website content.
When I submit a particular URL I need to get the places where the web content is used or manipulated.
Is there any API to do that?
I'm using Copyleaks API to check for plagiarism, it fully supports C# and .Net applications. The API is based on HTTP RESTful architecture. It's pretty simple to integrate and work with.
Create a new visual studio project and install Copyleaks API Nuget Package.
To scan your content for plagiarism simply call the function 'scan' with your credentials. Here is a sample code (from their open source GitHub SDK):
public void Scan(string username, string apiKey, string url)
{
// Login to Copyleaks server.
Console.Write("User login... ");
LoginToken token = UsersAuthentication.Login(username, apiKey);
Console.WriteLine("\t\t\tSuccess!");
// Create a new process on server.
Console.Write("Submiting new request... ");
Detector detector = new Detector(token);
ScannerProcess process = detector.CreateProcess(url);
Console.WriteLine("\tSuccess!");
// Waiting to process to be finished.
Console.Write("Waiting for completion... ");
while (!process.IsCompleted())
Thread.Sleep(1000);
Console.WriteLine("\tSuccess!");
// Getting results.
Console.Write("Getting results... ");
var results = process.GetResults();
if (results.Length == 0)
{
Console.WriteLine("\tNo results.");
}
else
{
for (int i = 0; i < results.Length; ++i)
{
Console.WriteLine();
Console.WriteLine("Result {0}:", i + 1);
Console.WriteLine("Domain: {0}", results[i].Domain);
Console.WriteLine("Url: {0}", results[i].URL);
Console.WriteLine("Precents: {0}", results[i].Precents);
Console.WriteLine("CopiedWords: {0}", results[i].NumberOfCopiedWords);
}
}
}
For more information, read their full tutorial.
I am afraid that such method doesn't exist.