use of "application/x-www-form-urlencoded" in HTTP Post method - http

What is the meaning of
ContentType = "application/x-www-form-urlencoded"
in HTTP Post method..??
my code is
Uri url = new Uri(" http://blah/blah/blah...json");
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";

I have been using ContentType = "application/x-www-form-urlencoded" in particular project. What I know is, it is the scheme that is used, so the POST parameters you are sending to the server are in the form-urlencoded. For example, if you send the parameter key-value like below:
name=Agung
id=121
then, when you send the request, the POST body will be like encoded to be like this:
name=Agung&id=121
When you are setting ContentType = "application/x-www-form-urlencoded", then the server will know how to parse the Body parameter you are sending.
Hope it will help

This is used for Security purpose.
you can get more help here.

Related

How would one send an HTTP POST request?

I'm using lua-http for HTTP requests in my Lua script. I'm trying to find a way to send data as a POST request, similar to the -d option of curl.
I've tried new_from_uri:set_body() but I don't think I'm doing it correctly.
request = require "http.request"
headers, stream = assert(request.new_from_uri("https://example.org"):set_body("body text"))
headers, stream = assert(request.new_from_uri("https://example.org"):go())
body = assert(stream:get_body_as_string())
if headers:get ":status" ~= "200" then
error(body)
end
Could someone show me how to do this properly?
I've decided to use luasocket for this instead. Here is the code I'm using:
http = require "socket.http"
body = "body text"
respbody = {
result, respcode, respheaders, respstatus = http.request {
method = "POST",
url = "https://example.org",
source = ltn12.source.string(body),
headers = {
["content-type"] = "application/json", -- change if you're not sending JSON
["content-length"] = tostring(#body)
},
sink = ltn12.sink.table(respbody)
}
respbody = table.concat(respbody)

Does get request need content type and accept header?

I am making web request and method is get.Does I need to mention content-Type and accept for get method?or it just require for post method
string strURL = "web address";
Uri uri = new Uri(strURL);
HttpWebRequest webRequest = System.Net.WebRequest.Create(uri) as HttpWebRequest;
webRequest.Method = WebRequestMethods.Http.Get
webRequest.ContentType = "application/json";
webRequest.Accept = "application/json";
content-type : It is not required in the GET request as you are not sending any content in the request body.
content-type indicates the media type of the entity-body sent to the recipient.
Accept : It depends on your requirement. If you want to restrict the media type in your response then you can use it otherwise leave it.
The Accept request-header field can be used to specify certain media types which are acceptable for the response.

Get HTTP Response from a url by using C#

Environment: ASP.Net MVC 4 using C#
I need to get image by using GET request to a URL /inbound/faxes/{id}/image
I used the code below
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("/inbound/faxes/238991717/image");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader stream = new StreamReader(response.GetResponseStream());
but it flags "URL not valid"
I used the complete URL www.interfax.net/inbound/faxes/{id}/image
but the result is same
I want to follow this article to receive faxes
Accepting incoming fax notifications by callback
Can anyone help me to get fax...?
Try like this:
using (var client = new WebClient())
{
byte[] imageData = client.DownloadData("http://www.interfax.net/inbound/faxes/{id}/image");
}
Notice how the url is prefixed with the protocol (HTTP in this case). Also make sure you have replaced the {id} part of the url with the actual id of the image you are trying to retrieve.

HttpWebRequest Hanging

In the application I am currently working on there is a backend java app that is caching a bunch of data. The asp.net part is allowing users to update database tables. Each time the DB is updated the cache in the java application should be cleared. So basically I have a list of 4 URLs that each need to be hit in order to clear the cache. My basic solution was to loop through each url and create a HttpWebRequest and get then get the response. So basically I have this for each request:
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = 0;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string responseString = readStream.ReadToEnd();
returnList.Add(string.Format("Refresh response from {0}.<br />{1}", url, responseString));
readStream.Close();
receiveStream.Close();
}
On my local machine everything works great. But when I deploy to our development server it just hangs and does nothing. If I remove request.ContentLength = 0; then the remote server throws a 411: Length expected error.
I am really stuck here and any help would be greatly appreciated.
Either a solution to the HttpWebRequest problem I am having or a different solution to calling each URL would work, I'm not picky.
Thanks in advance.
Why are using request.method as "POST"? Are you posting any data, if not try removing both content length and request method.
Pretty sure this was a network issue. I tried hitting a different url (the load balancer) and had no problems so the java guys are making a changes so I can just hit the load balancer and whatever server the request ends up on will make sure all servers caches are cleared.
The code that is working:
var request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string responseString = readStream.ReadToEnd();
returnString = string.Format(#"Refresh response from<br />{0}{1}", url, responseString);
readStream.Close();
receiveStream.Close();
}

How to add header for http request

I am new to Restlet development, trying to add headers to do a HTTP request. I tried the following code, but got "400 bad request, the header is not valid"
String url = "http://xxxxx";
Client c = new Client(Protocol.HTTP);
Request request = new Request(Method.GET, url);
HashMap attributes = new HashMap();
attributes.put = ("DeviceID", "myDeviceID");
attributes.put = ("Centent-Type", "myCT");
attributes.put = ("User-Agent", "my user agent");
attributes.put = ("ClientID", "myCid");
request.setAttributes(attributes);
Response r =c.handle(request);
I am using Restlet 2.0.
Please help. any sample code would be great help. thanks in advance.
KC
HTTP protocol has a list of allowed headers: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
ClientID and DeviceID don't seem to be allowed headers. If you want custom headers you should prefix them with "X-".
Try using
attributes.put = ("Content-Type", "myCT");
Altough there might be other problems as well (myCT content-type?). Never used ClientID and DeviceID header also... but I'm a PHP guy :)

Resources