Sending OK Response over HTTP to a webpage request - asp.net

I am using an SMS Gateway to make my application receive SMSs. For this, the SMS Gateway sends a request to one of the pages in my application with the message as a querystring parameter. eg. http://myapplication/SMSReceiver.aspx?Message=PaulaIsHome.
Now after my page gets invoked, I need to send an OK response to the SMS Gateway so that it doesn't keep retrying to send the same message to my application again and again. I cannot figure out how to send the OK response.
I am using ASP .Net and C#.
Thanks

You are invoking an ASPX page, so I am guessing that a bunch of HTML is being returned in the response. Use a generic handler instead (this is what they are for), and then you can easily control all of the output...
context.Response.ContentType = "text/plain"
context.Response.Write("OK")
context.Response.End()

Related

How to post to an internal URL in classic asp from server

How to post to an internal URL in classic asp from server?
Using MSXML2.ServerXMLHTTP looks not work per this and this
I cannot use Response.Redirect since I need to send http code 200 right after I receive the request
I cannot use either Server.Execute or Server.Transfer since I want to modify Form collection before posting to destination internal URL
Thank you
Update:
I create an classic ASP endpoint to accept request from a third party. And I need to send back a response with HTTP status code 200. Then this endpoint doesn't process anything but needs to post the request to an internal endpoint for processing. Hope this clear now.
Khoi

Handling bad request in asp.net web api

I have a api url like below in my mvc4 app
http://localhost:15839/api/mydata/getdata/3365895543/PROBLEMDATA/myotherparam
Now client is consuming the above url to send httprequest. In response api is sending back a response. but in PROBLEMDATA of the url user is sending bad characters that are giving me Bad Request - Invalid URL. I can't force my client source to encode data. i need to handle it in my web api and give back my client a string "Unsucessful". I have seen this webapi cycle wondering at which point I should handle this. probably at http message handler but How?
I may need to follow this. but Register(HttpConfiguration config) also doesn't get hit
I believe you can capture this globally by overriding the application_error method. From there I suppose you could produce the "unsucessful" response or pass the request along to be handled at the controller level.
Take a look at this question as well.

Error Message for failed POST requests

I want to display appropriate error message when a connection is lost with a server. During a GET request the browsers "Connection Lost" message is displayed but when a POST request is made and no response is displayed the page doesn't respond properly. I am developing the page in ASP.net
Hope to hear fast responses.
Thanks in advance.
HTTP is not designed to be connected, it is stateless. before ASP.NET SingnalR was born, everything is just request-response. No matter GET or POST, the server should always send response to the client.
A "failed" post, should also be responded but with a status code like 500. In your case, you want to detect if a network connection is cut off, so the "failed" will mean "timeout" for you.
From server side, you cannot control it. So you can only do it on client side. such as using JavaScript to send a POST request, and if the server did not response in 15s, you can call it "connection lost", and display a message on your webpage.
Facebook chatting window is using this way. It will have a javascript function running on client browser, and keep "ping" the server to tell if the connection is OK.

Request and Response in asp.net

As per my understnding the difference between Response and Request is below
Request is - We request to server for like .aspx page
Response is - We get the .aspx page from server
So, I think, request is toward Server and response is what we got.
We have following terms
Request.QueryString
Request.RawUrl
Request.MapPath()
All these seems to go to server first and brings back the associated data. But the following term is contrary ?
Request.Cookies
Because the cookies creates at client side and value part is also fetched at client side using Response.Cookies
Your comments?
Query - 2 - Why it is useful to create/Access cookie using Request/Response.cookies? Because it can be created/fetched at client end in JavaScript.
Query 3 - Cookie resides at client end. Why do we send request to server ?
Query - 4 - Why do we write Response.Cookies? to go to server? Why? it creates at client end and accessed from client end. right? Why do we write Request.Cookies? Means fetching cookie information from server? Cookie is at client end. right?
"When a browser makes a request to the server, it sends the cookies for that server along with the request. In your ASP.NET applications, you can read the cookies using the HttpRequest object, which is available as the Request property of your Page class. The structure of the HttpRequest object is essentially the same as that of the HttpResponse object, so you can read cookies out of the HttpRequest object much the same way you wrote cookies into the HttpResponse object."
ASP.NET Cookies Overview
"Cookies are sent to the browser via the HttpResponse object that exposes a collection called Cookies. You can access the HttpResponse object as the Response property of your Page class"
Beginner's Guide to ASP.NET Cookies
Every time you send a Request to server, the cookies for that server are also sent.
Also, when the server sends you a Response it can include cookies for the next Request you send it to.
So Request.Cookies and Response.Cookies make perfect sense.
Both objects Request and Response "live" in the server. So Request holds the data sent by the User Agent (the Browser, like Chrome, IE, etc.). Examples of this data are, the POST and GET Variables, the User Agent, the language, IP Adress, and many more.
Response is the object that lets you send data to the User Agent (the browser), i.e. a Web Page, a stream of bytes (like a downloadable file), etc.
The cookies live in the client side, that's right, but is the browser that send this information, so this data comes in the Request object.
You receive the cookies via Request.Cookies, but you receive the cookies in the Server. If you are coding in C#, the code is in the Server point of view, so receive means, the server receives. If you want to access the cookies in the Client Side, you must use some client programming language like JavaScript.
I hope this helps.

.Net System.Net.HttpWebRequest & Authentication Cookies

I would like to know if the following is possible:
As part of the PageLoad of an ASP.Net page request:
Use HttpWebRequest (POST) to make a request to a forms authentication
webpage of a different web application
Examine the response to see if the authentication succeeded (identifiable if an authentication cookie has been set)
If it fails, then finish the ASP.Net page request, which will display a message saying "couldn't auto-login" or some such thing
If success, then instead of completing the ASP.Net page lifecycle, use "Response.Write" to send the response from the HttpWebRequest back to the browser (thus essentially displaying the webpage from the HttpWebRequest)
If the above is possible, then, is there also a way to transfer the authentication cookie from the HttpWebResponse to the requesting browser? My gut feeling is probably not - but if this is the case, then how do proxy servers handle such things? Is that not essentially what they are doing?
Thanks for any help
You can add a CookieContainer to the HttpWebRequest. It will store any cookies received from the response and if you attach the same cookie container to the next request, it will send those cookies back up to the server.
You can access the contents of this CookieContainer object and send those back to your client using Response.Cookies.

Resources