Parse stdClass object in C# - c#

I'm currently working on a client that has more features than another existing client for a game. One of the functionalities I would like to add is the ability for a user to add an IP address to their favourites and display relevant information of that server (IP, Port, Hostname, Gamemode etc.).
In order to do this I use an API which takes several parameters, such as the IP address and port. It returns some kind of string which contains the information that I need. However I'm having trouble extracting the information I want from the string. I did manage to do it, but I did it quick and dirty by using a dozen substrings to cut out the values of the parameters from the response.
Here's an example of an API call: http://monitor.sacnr.com/api/?IP=149.202.89.123&Port=7777&Action=info
Initially I thought the response was some kind of JSON string, but after some googling I found a question on Stackoverflow about stdClass. Apparently it's some kind of generic PHP class.
So my question is: how can I convert the response of the API to say JSON or XML, so I can easily parse it?
Example response:
O:8:"stdClass":18:{s:8:"ServerID";s:7:"1780392";s:2:"IP";s:14:"149.202.89.123";s:4:"Port";s:4:"7777";s:8:"Hostname";s:32:"Next Generation Roleplay (0.3.7)";s:8:"Gamemode";s:14:"NG:RP v3.0.409";s:8:"Language";s:7:"English";s:3:"Map";s:13:"NG-Gaming.net";s:10:"MaxPlayers";s:3:"500";s:7:"Players";s:3:"131";s:7:"Version";s:8:"0.3.7-R2";s:8:"Password";s:1:"0";s:4:"Time";s:5:"20:00";s:6:"WebURL";s:13:"ng-gaming.net";s:4:"Rank";s:3:"112";s:10:"AvgPlayers";s:5:"88.27";s:9:"HostedTab";s:1:"1";s:10:"LastUpdate";s:10:"1486138780";s:12:"TotalServers";s:4:"1305";}

Try using the Sharp Serialization Library.
As per their description: "Sharp Serialization Library serializes and deserializes primitives, ArrayLists and Hashtables, compatible with PHP serialize()"

Related

How do I send a POST Request to Google Translate Api using a C# WebClient?

I googled now for almost an hour and can't find anything that would help me out. I'm a beginner programer and took the course over from TeamTreeHouse with Serialization in C#. Here I also learned how to use the WebClient.
Now I thought a good practice Project would be to make a Translate app that sends to google the user input in a POST Request and Google returns the answer as Json which I deserialize.
Problem is I read through the documentation of the api but I'm so confused of what I should send exactly to google and really how to do this?
I know the method webclient.Headers.Add(arguments here) , but I really don't know what else it needs.
You can find an example over here:
https://cloud.google.com/translate/docs/translating-text
https://translation.googleapis.com/language/translate/v2 Three query
parameters are required with each translation request:
Target language: Use the target parameter to specify the language you
want to translate into. Source text string: Use the q parameter to
specify each text string to translate. API key: Use the key parameter
to identify your application. If you are using OAuth 2.0 service
account credentials (recommended), do not supply this parameter.
So problem is there is no placeholder in that example URL where I could put my api key + soure text string an Target language.
So what exactly should I send to google so it knows what I want and returns me the JSON file?
Maybe anyone could help me out. And I know there is an official Library for exactly this but I want to practice serialization and using web scraping with the WebClient class so I want to do it like this.
You specifically asked about a POST.
You can use this url:
https://translation.googleapis.com/language/translate/v2?key=MY_KEY
Of course, replace MY_KEY with your key.
Add a header for the content type:
application/json; charset=utf-8
Format your text and target language as JSON and write it to your request stream:
{"q":"Team work is a major progress maker at this location, it appears that everyone is willing to help when they can.","target":"fr"}
The documentation here: https://cloud.google.com/translate/docs/reference/translate#body.QUERY_PARAMETERS
says use query parameters q, target, key, etc.
So your URL should be something like https://translation.googleapis.com/language/translate/v2?key=[yourAPIkey]&target=language

Parsing Data in Xamarin Forms

I would like to get the names in collection href(CustomerDemographics, Customers, Employees, Order Details) to populate a listview. I would like to know how to parse these information in Xamarin platform or C#.
I am quite new on this environment. I have experience how to parse json data in native ios with objective c, but it is the first time I see this type of data.
From the attached image it seems that you're requesting the service document of the OData service. Namely the http://host/service/ endpoint of the service. If you would like to get the content of the collections, you should append the names of the collections to the end of the service document URL, such as:
GET http://host/service/Categories
GET http://hsot/service/CustomerDemographics
The format of the response payload depends on the protocol version of the OData service. If you are talking to a OData V4 service (it actually seems a lot like you are querying the Northwind OData V4 sample service: http://services.odata.org/v4/northwind/northwind.svc/), the response payload will be in JSON format and you can use the ways that you are familiar with to parse the response.
In addition, I would recommend you go through the basic tutorial on OData.org so that you can get a better grasp of OData requests: http://www.odata.org/getting-started/basic-tutorial/

Can unpredictably typed arguments be handled in a WCF service?

I'm relatively new to WCF services, so I apologize in advance if I'm missing the obvious. My business uses EasyPost as our shipping solution, and I've written a WCF service to handle the shipping status webhook calls from EasyPost, described here: https://www.easypost.com/docs/webhooks
Briefly, EasyPost sends an update object as JSON via POST. The problem is that it sends several different types of (non-configurable) updates to the same service method, and I'm finding it difficult to write a DataContract that encompasses all the possibilities. For example, if the argument it sends is a tracking number update, update.result.status will be a string value; if it's a batch status update, update.result.status will be an object. It's a bit of a mess.
I tried handling just the update type that I care about and returning a 400 status code on the others, but EasyPost interprets that as an outage and drops my service as a webhook endpoint.
From what I've read, it looks like I could abandon the comforts of a Data Contract in favor of using a System.ServiceModel.Channels.Message parameter as a catch-all, then parse the message manually. That doesn't strike me as a wise/clean solution, though.
I'd be grateful for any alternatives.
This is probably not the best way I could have handled this, but it works.
I have an HTTP Module that identifies whether the incoming request is for the proper service method, and if so, converts the ContentType header from "application/json" to "text/plain".
My service method accepts the content body as a System.IO.Stream parameter. By converting the stream to a byte[] and then to a string, I end up with the raw JSON string sent by EasyPost.
After that, it's just a matter of using Newtonsoft.Json to attempt to deserialize the JSON string into the expected Type.
Even if the deserialization fails, I can still log the data and send a success response to the caller. That's good enough for my purposes.

how to get the project specific jira issues using jira rest api in .net?

I have a requirement where i need to get all the issues for a particular project in jira so for this i have created a console application which has rest client class using which I make a GET request call and for testing purpose rest api url is
"https://jira.atlassian.com/rest/api/latest/issue/JRA-9"
using this url i make a HttpWebRequest and get the response back in json formated string. Now this json string contain all the issue specific information but my actual requrement is to get all the project specific issues.
I tried to find out if i get any project specifc URL for testing purpose from where i get json reply back and I found http://kelpie9:8081/rest/api/2/search?jql=project=QA+order+by+duedate&fields=id,key but for this i get the "The remote name could not be resolved: 'kelpie9'" error.
Could you please help me in this?
`
JIRA's REST API does not appear to currently support any project-based queries separate from their search API.
You can specify a specific project in the search by using the JQL. Given that you know a project (e.g., "JRA" in "JRA-9"), then you can quickly search through all of its issues:
Working result: https://jira.atlassian.com/rest/api/latest/search?jql=project=JRA
One important note is that the results return actual total versus what is actually returned:
"startAt":0,"maxResults":50,"total":30177
You can add query string variables to the request to get more (or less) results. You can also control the fields related to issues to retrieve as well: https://jira.atlassian.com/rest/api/latest/search?jql=project=JRA&startAt=75&maxResults=75 (slower the more you request, and probably not nice to hit their public servers with big numbers).
You can even POST a JSON object that represents the query (slightly tweaked from the linked search docs):
{"jql":"project = JRA","startAt":75,"maxResults":75,"fields":["id","key"]}
Of interest, and as part of the JQL, you can sort the results by any field. Just add " order by id" to the project name, as-in "jql=JRA+order+by+id" in the querystring or "jql": "project = JRA order by id" in the POSTed JSON body.
Note: Above is the actual answer to the real question. However, the literal question is the cause of the `The remote name could not be resolved: 'kelpie9' error.
Their documentation shows kelpie9 as an example server name that they are testing on internally, running on port 8081. Your computer is not aware of a server/machine named kelpie9, as it does not publicly exist. Replace kelpie9 with whatever your JIRA server's hostname is internally and 8081 with whatever port it is using (or remove it if you do not see one when you view JIRA on your intranet site, which means port 80 for http and port 443 for https). For example, many companies run it a "https://jira/". You would replace the example link with https://jira/rest/api/2/search?jql=project=QA+order+by+duedate&fields=id,key.

Server side PHP sql queries to C# app

I'm retrieving some userdata from a phpbb3 forum through scripts on the server.
My database does not allow for external login, so I have to go through some kind of script.
But, I'd like to know if there is a safe enough way to retrieve this data?
I know how to output some XML with echo statements, but I'm not sure if this is as secure as it should be?
Optimal method would be (psuedo code)
$array['user_id'] = $id;
$array['otherinfo'] = $var
return $array;
if I could somehow read this array with C#, it would be much easier, but I'm not sure how this would work with WebResonses or whatnot.
Any ideas?
You need a web service, that will output your responses in some format that you can parse with C#.
Php and C# are not interoperable, that s why you will need to create a service that you can consume these messages.
First option as I said is to create a web service, that C# code can consume. In this case, your C# code should know what to ask from your php web service, and you will respond to the request, in XML, or JSON or whatever format you want to use within your C# code to parse it.
Another option is to push the data to a web service that uses C#. You need to write a WCF/ Web service, and you can push the data to this service.
I suggest you to give your array output as xml or json. If we take xml as consideration, you can call the php file which gives xml output like this:
XDocument.Load("http://whatever.com/whatever.php");
And ofc you need to add the System.Xml.Linq header to use XDocument class. And also you can pass a hashed key, etc. from query string to validate the requested is coming from your app.

Categories

Resources