Consume web service using post in c# - c#

Currently I have less information about this, but I will update soon with more details.
I have been provided with a web page which provides user name & password option to access a url & return a token value.
The form contains that url in action & the method used is POST.
I have tried general HttpWebRequest methods to access the url, but have been unsuccessful so far. It gives error "cannot connect to remote server"
The url is like
www.somesite.com\method.php\level1\method1\xml
The url confuses me as other links have some extension at the end(I am relatively new to this web service)
I have been told this webservice has been consumed in mobile.
Can someone please guide me as to how to access a web service in c# using POST method?
I apologise for the lack of detailed information

First try to understand how the webservice works.
You can simply create a simple c# rest web service. To return json data , you will need to specify the return data format on specific operation in the web service. To use mysql for database, you can search for mysql connector for c# on google. You can find libraries to use in your code. Hope it helps !
http://www.codeproject.com/Articles/201901/CREATE-RESTful-WCF-Service-API-Using-POST-Step-By

I was able to access by providing proxy details.

Related

c# web api as jira webhook url

I have been tasked to implement a JIRA SMS notification(using Twilio) similar to its in-built email notification. I know this can be easily done using Zapier and Twilio.
But i am told to look for a workaround with out using Zapier.
The only option i think of now is to create a WebApi project that calls Twilio to send the SMS, host the WebApi project on a server and pass this URL as a WEBHOOK in JIRA.
For this i have created a C# class library which calls Twilio.
class
and after that i have added a new WebApi project for this solution and added the above class project reference.
and i have added a new controller:
controller
i am just calling the send method from the constructor with out any parameters which i think will do the job just from hardcoded number to a given number(in the controller.
but what i need is to pass in messageBody and recipientPhoneNumber from the JIRA webhook to this controller. Not sure how to do this.
As i am very new to WebApi's could some one please help me with the code. much appreciated.
Twilio Dev Evangelist here...
Mind dropping me a note at corey#twilio.com? I agree with the previous responses - this site may not be the best forum to support what you're looking for here, but I would be more than happy to directly help you out here.
but what i need is to pass in messageBody and recipientPhoneNumber from the JIRA >webhook to this controller. Not sure how to do this.
The way webhooks work is a little bit different than your assumption above. You cannot pass anything to JIRA webhook. JIRA POSTs data to the webhook (your API URL). Once you receive the data in your API controller action, you can construct a message body and recipient phone number (from e.g. user object passed in by JIRA; if that object doesn't have any phone number attribute, you might have to use a custom field).
As i am very new to WebApi's could some one please help me with the code. much appreciated.
No problem. I guess you have done the Twilio part alright already. Please visit MS docs to know more about Web API. You have to add a controller method so that it can be called by JIRA. So your final URL becomes this. You can then put this URL in JIRA as webhook. Replace hostname mycompany.com by your server hostname and change other url segments based on your situation.

Unknown WebService description and consume it from C#

I'm quite a noob and have been developing 'seriously' (as my job) from a couple of months barely, so i apologize in advance for my ignorance.
There is this web service that i need to consume from a C# client on an aspx page I want to develop, but first I need to understand the webservice, I'm not sure which language it's made upon but I think it's PERL, since the web service's URL is like this "http://wschsol.mideplan.cl/mod_perl/xml/fps-by-rut". This webservice was developed by other people which i cannot contact right now and is running on a linux server to which i don't have access either.
The webservice's job is pretty simple, it receives a person's national ID number and returns some information about him on xml format, which i want to show on my client aspx page with some grids and stuff.
I have read around the internet that it's possible to see a description of a webservice and its methods using the WSDL variable after the common ".asmx" extension, but in this case there is no extension and so, i can't use the the ?WSDL. I'm guessing that maybe "fps_by_rut" is only a webmethod, and not the webservice itself. So the question is: how do I use the webservice?
Since I know what kind of request is expected (a person's ID), I tried to manually add an ID to the URL through the browser (like this: "http://wschsol.mideplan.cl/mod_perl/xml/fps-by-rut?rut=6985462-1") and if I do it responds normally in xml format.
I tried to add a web service reference for it on my project, but well, i pasted the whole URL and when I click "go" it says it needs credentials. I have these credentials, a user and password, but they are not working... what confuses me is that there is another client to this same webservice programmed on classic asp made by the guy before me here, and i can acces that code, and when i see the line on which he calls the web service it's like this:
xml.Open "GET", "http://wschsol.mideplan.cl/mod_perl/xml/fps-by-rut?rut="&rsVac(0), False,"user","password"
i have censored the "user" and "password" strings since those are the actual credentials. This classic asp client works fine with those credentials. I tried to use those when creating the reference, but they are not working. Even more, when i manually added the ID through the browser it asked me for credentials and they worked too...
Am I going the wrong way? Please guys, i need guidance. If there is a course out there which I can read that helps me understand all of this webservices stuff, i'd be hugely grateful. Or if someone can tell me which way to go, I'm pretty sure I'm in the wrong direction...
Thanks in advance for any help!!!
WSDL is used for SOAP and we both don't know if it's an SOAP-Service.
You should just use a HttpClient and make your Get-Calls to the API.
You can use something like this:
var client = new HttpClient();
client.BaseAddress = new Uri("http://wschsol.mideplan.cl");
var httpResponse = await client.GetStringAsync("mod_perl/xml/fps-by-rut?rut=<InsertParamHere>");
Edit Authorization:
You have to add this:
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
Snippet source: Simple C# .NET 4.5 HTTPClient Request Using Basic Auth and Proxy

How can a WCF Service obtain Query Parameters?

I'm working on an Azure service for a Windows Phone app. I need the Azure Service to access the users' OneDrive. Following this article, my scenario should be:
The user sign in to Windows Live on the WP app.
The Live web service sends the authorization code to a redirect URI that I defined, with the code appended as a query parameter named code, as:
http://www.example.com/callback.php?code=2bd12503-7e88-bfe7-c5c7-82274a740ff
I get the authorization code and access the users' data
After investigating a lot in Service, I still can't find a way to capture the query parameter in my web service. As I am new to this area, I don't know where to focus on. I'll be really appreciated if you can give my an advise or answer my following questions:
Can I access the service just using the url with parameter in a browser? How can I see if the service is working properly?
An article mentioned using WCF [Web Get] attribute to get Query Parameters, but I still don't know how to implement both the IService1.cs and Service1.cs file, could you give me a sample about how to access the value of Query Parameter?
Thanks!
I'm not sure if i understand your problem properly but if you want your RESTfull WCF service to be the callback receiver for the request code, your Service must be hosted with a WebHttpBinding and a ServiceContract similar to this one.
[ServiceContract]
public interface IService
{
[WebGet(UriTemplate = "callback?code={requestCode}")]
void OAuthCallback(string requestCode);
}
So if the base address of your Service is "http://service.mydomain.com/MyService.svc" the OAuthCallback Method will be called when a http GET request to "http://service.mydomain.com/MyService.svc/callback?code=RequestCode" is made.

Handling Authentication for a File Display using a Web Service

This is my first time developing this kind of system, so many of these concepts are very new to me. Any and all help would be appreciated. I'll try to sum up what I'm doing as efficiently as possible.
Background: I have a web application running AngularJS with Bootstrap. The app communicates with the server and DB through a web service programmed using C#. On the site, users can upload files and reference them later using direct links. There's no restriction to file type (yet), so just about anything is allowed.
My Goal: Having direct links creates a big security problem for me, since the documents/images are supposed to be private data. What I would prefer to do is validate a user's credentials when the link is clicked, then load the file in the browser using a more generic url path.
--Example--
"mysite.com/attachments/1" ---> (Image)
--instead of--
"mysite.com/data/files/importantImg.jpg"
Where I'm At: Not very far. My first thought was to add a page that sends the server request and receives a file byte stream along with mime type that I can reassemble and present to the user. However, I have no idea if this is possible using a web service that sends JSON requests, nor do I have a clue about how the reassembling process would work client-side.
Like I said, I'll take any and all advice. I'd love to learn more about this subject for future projects as well, but for now I just need to be pointed in the right direction.
Your first thought is correct, for it, you need to use the Response object, and more specifically the AddHeader and Write functions. Of course this will be a different page that will only handle file downloads, so it will be perfectly fine in your JSON web service.
I don't think you want to do this with a web service. Just use a regular IHttpHandler to perform the validation and return the data. So you would have the URL "attachments/1" get rewritten to "attachments/download.ashx?id=1". When you've verified access, write the data to the response stream. You can use the Content Disposition header to set the file name.

accessing websites using C#

I have a problem here. Assume there's a basic calculator implemented in javascript hosted on a website ( I have googled it and to find an example and found this one: http://www.unitsconverter.net/calculator/ ). What I want to do is make a program that opens this website, enters some value and gets the return value. So, in our website calculator, the program:
- open the website
- enters an operand
- enters an operation
- enters an operand
- retrieve the result
Note: things should be done without the need to show anything to the user ( the browser for example ).
I did some search and found about HttpWebRequest and HttpWebRespond. But I think those can be used to post data to the server, which means, The file I'm sending data to must be php, aspx or jsp. But Javascript is client side. So, I think they are kind of useless to me in this case.
Any help?
Update:
I have managed to develop the web bot using WebBrowser Control tool ( found in System.Windows.Forms )
Here's a sample of the code:
webBrowser1.Navigate("LinkOfTheSiteYouWant"); // this will load the page specified in the string. You can add webBrowser1.ScriptErrorsSuppressed = true; to disable the script in a page
webBrowser1.Document.GetElementById("ElementId").SetAttribute("HTMLattrbute", "valueToBeSet");
Those are the main methods I have used to do what I wanted to.
I have found this video useful: http://www.youtube.com/watch?v=5P2KvFN_aLY
I guess you could use something like WatiN to pipe the user's input/output from your app to the website and return the results, but as another commenter pointed out, the value of this sort of thing when you could just write your own calculator fairly escapes me.
You'll need a JavaScript interpreter (engine) to parse all the JavaScript code on the page.
https://www.google.com/search?q=c%23+javascript+engine
What you're looking for is something more akin to a web service. The page you provided doesn't seem like it accepts any data in an HTTP POST and doesn't have any meaningful information in the source that you could scrape. If for example you wanted to programmatically make searches for eBay auctions, you could figure out how to correctly post data to it eg:
http://www.ebay.com/sch/i.html?_nkw=http+for+dummies&_sacat=267&_odkw=http+for+dummies&_osacat=0
and then look through the http response for the information you're looking for. You'd probably need to create a regular expression to match the markup you're looking for like if you wanted to know how many results, you'd search the http response for this bit of markup:
<div class="alt w"><div class="cnt">Your search returned <b>0 items.</b></div></div>
As far as clientside/javascript stuff, you just plain aren't going to be able to do anything like what you're going for.
It is a matter of API: "Does the remote website expose any API for the required functionality?".
Well web resources that expose interactive API are called web service. There are tons of examples (Google Maps for istance).
You can access the API -depending on the Terms & Conditions of the service- through a client. The nature of the client depends on the kind of web service you are accessing.
A SOAP based service is based on SOAP protocol.
A REST based service is based on REST principles.
So, if there is an accessible web service called "Calculator", then you can access the service and, for istance, invoke the sum method.
In your example, the calculator is a Javascript implementation, so it is not a web service and it cannot be accessed via HTTP requests. Though, its implementation is still accessible: it is the javascript file where the calculator is implemented. You can always include the file in your website and access its functions via javascript (always mind terms and conditions!!).
A very common example is the jQuery library stored in Google Libraries.

Categories

Resources