How to get request url who used my webapi in asp.net? - c#

I already write a Web API method, now I want to get a client website URL who used my API method.
I tried this line of code but I get my API URL. But I want a client URL.
var requestUrl = Request.Url;

var clientRequest = HttpContext.Request.Headers["Referer"];

Related

HTTPS availablity of http url

I am having list of http urls, I need to find https url is available or not. Example : http://www.apra.gov.au/Insight/Pages/insight-issue2-2017.html, need to check whether https is available on the same domain via c# code. since I have a list of 5k http urls. I need to verify all these url available on HTTPS?
You can probably do a simle string replace (http: = https: ) and then loop through them all calling httpget to check:
Psuedo code:
var httpClient = new HttpClient()
foreach(var url in urls)
var httpUrl = url.Replace("http:","https:");
httpClient.Get(url);

Get instance of ApiController class from a URL directly

I am looking for a way to call the appropriate method (get, post etc.) on an ApiController class based on the URL and request type etc. without making a http request.
Background: We have an API application with numerous controllers that needs to also accept requests from a remote server. Due to restrictions I cannot control there is no way to open ports between the two servers to allow the remote server to make the request directly so we decided to forward the data using websockets (SignalR). I can send (within reason) whatever information is required.
I have tried the below:
HttpRequestMessage request = new HttpRequestMessage();
var bld = new UriBuilder
{
Port = 123,
Path = "api/v1/search",
Query = "query=search_string"
};
request.RequestUri = bld.Uri;
var httpCfg = AppConfiguration.Get().HttpConfig; //this is the same config that UseWebApi was called with and contains the routes.
var route = httpCfg.Routes.GetRouteData(request);
var controllerSelector = new DefaultHttpControllerSelector(httpCfg);
var descriptor = controllerSelector.SelectController(request);
route contains the controller name (search) but the call to SelectController throws an exception with a 404 response in it (I presume this indicates I am missing something from the fake request). The same URI works when sent as a direct http request so the routes do work as best I can tell.
Is there a better way to do this, or if not what am I missing from the request that is causing the 404?

MVC 5 Facebook Publishing Feed

I have been playing around with the open graph api and more or so with the Publishing part as mentioned here
https://developers.facebook.com/docs/graph-api/using-graph-api/#publishing
It mentions For example, to publish a post on behalf of someone, you would make an HTTP POST request as below: I require the UserID and Access Token.
So I have been able to get the Access Token which doesn't expire and also the User ID of the user that has accepted the app to publish content.
However to my failure I am unable to post feed by using the steps mentioned in the above link.
This is a small example I put together to test the HttpPost Request
[HttpPost]
public ActionResult FacebookPostResponse(string accessToken)
{
string fbPost = "Hello";
Uri targetUserUri = new Uri("https://graph.facebook.com/10153688496941651/feed?message=" + fbPost + "&access_token=" + accessToken);
HttpWebRequest post = (HttpWebRequest)HttpWebRequest.Create(targetUserUri);
HttpWebResponse res = (HttpWebResponse)post.GetResponse();
var sC = res.StatusCode;
ViewBag.Message = sC;
}
The ActionResult above returns me a status code of OK meaning the request was successful. However when I go to my facebook wall I don't see anything from the app?
When I copy the URL request in a web browser it returns the following:
{
"data": [
]
}
I am not sure what I am doing incorrect? Anybody have suggestions?
You should set up your HttpWebRequest instance to use POST http method, the default value is GET
so just add this:
post.Method = "POST";
After :
HttpWebRequest post = (HttpWebRequest)HttpWebRequest.Create(targetUserUri);

how to call webservicemethod in windows service

Basically my idea is to develop a proxy which will run in windows
I have created windows service application which running successfully and i have integrated a web service code in the windows service application running in windows service.
How to call that web service method when the client hits my url?
How to form the url which can call web service method to get the method return value?
OK, I'll try to answer.
Let's assume you want to call REST web service. What do you need? A HttpClient and (probably) JSON/XML Serializer. You can use built-in .NET classes, or a library like RestSharp
Sample calling REST web service using RestSharp
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
RestResponse 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();
You are not required to use RestSharp, no. For simple cases HttpWebRequest (+DataContractJsonSerializaer or Xml analogue) will be just perfect
Having SOAP web service?
Follow the instructions provided here

Get And Use Facebook Access Token Without HTTPS

I have the following url for the first stage. getting code http://www.facebook.com/dialog/oauth?client_id=myappid&redirect_uri=myurl;state=e879888c-7090-4c09-98a5-ff361b30d55;scope=email
and
url = String.Format(#"http://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}",FacebookAppId, redirectUrl, FacebookSecret, code);
WebClient webClient = new WebClient();
var tokenVars = webClient.DownloadString(url);
var responseVars = HttpUtility.ParseQueryString(tokenVars);
string access_token = responseVars["access_token"];
string data = webClient.DownloadString(String.Format("http://graph.facebook.com/me?access_token={0}", access_token));
var jobject = JObject.Parse(data);
400 BAD REQUEST is returned.
Question: Is it possible to get facebook access token without connecting by http s ?
Question: Is it possible to use access_token without HTTPS? NOT HTTP
for a secure you must take https,
maybe if you aren't, some bad thing will show, #long process..
i just browse for your issue, and same thing

Categories