I have asked here how to make the https post, and now that works fine. Problem now is How to send a parameter, name query, which is a JSON string:
{"key1":"value1", "key2":{"key21":"val21"} }
What I'm doing and doesn't work is:
HttpWebRequest q = (HttpWebRequest)WebRequest.Create(Host + ":" + Port);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
q.Method = "POST";
q.ContentType = "application/json";
q.Headers.Add("JSON-Signature", GetFirma(query));
q.Credentials = new NetworkCredential(user,pass);
byte[] buffer = Encoding.UTF8.GetBytes("query=" + query);
q.ContentLength = buffer.Length;
using (Stream stream = q.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
But the server always answer saying there's no 'query' parameter. Any help?
In a POST request, the parameters are sent as a body of the request, after the headers. To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.
POST should not have query param. You can implement the service to honor the query param, but this is against REST spec.
Request parameters are used to send additional information to the server. A URL contains these parameters. There are two types of parameters: Query Parameter: These are appended to the end of the request URL, Query parameters are appended to the end of the request URL, following '?'
Under HTTP request settings: Enter a URL in the field provided. Select either Use HTTP GET or Use HTTP POST. Enter HTTPS instead of HTTP in the URL to send the information using HTTPS.
I would use WebClient.UploadValues
:
using (WebClient client = new WebClient())
{
NameValueCollection fields = new NameValueCollection();
fields.Add("query", query);
byte[] respBytes = client.UploadValues(url, fields);
string resp = client.Encoding.GetString(respBytes);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With