Unable to consume POST method of OpenShift REST based API - c#

I am trying to create application into OpenShift Red hat environment from my sample code which I have developed in C#.net.
string url =string.Format("https://openshift.redhat.com/broker/rest/domain/{0}/applications", cbDomains.Text);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Credentials = new NetworkCredential(USERNAME, PASSWORD);
string postData = "{\"name\": \"myapp\","
+ "\"cartridges\": [{"
+ "\"cartridge\": \"diy-0.1\"}],"
+ "\"scale\": false,"
+ "\"gear_size\": \"small\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded;";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
when I am executing above code I am getting following error:
The remote server returned an error: (422) Unprocessable Entity.
on this below line:
WebResponse response = request.GetResponse();
Please let me where I am doing wrong. Thanks in advance Jyoti

Related

How Could I Use Contentlength, GetResponse, or GetRequestStream In A UWP App (C#)

I know C# but am new to UWP... I have noticed that some HttpWebRequest definitions are non existent in UWP. How would I be able to use ContentLength, GetRequestStream, and GetResponse in a UWP application? Thanks. Here is my code if needed:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(SelectedItem + url);
webRequest.Headers.Add("SOAPACTION", SOAPACTION);
webRequest.Method = "POST";
webRequest.ContinueTimeout = 2000;
webRequest.ContentType = "text/xml";
string postData = xmlData;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
webRequest.ContentLength = byteArray.Length;
Stream dataStream = webRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Dispose();
WebResponse response = webRequest.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Dispose();
response.Dispose();
return responseFromServer;

Web Page Posting and Capturing Response using C#

I have a webpage :http://180.92.171.80/ffs/data-flow-list-based/. After Filling Basin Name and River Name from Drop-down Menu, Station Names are appeared in Flood Forecasting Sites, when I select any of them, it automatically redirect to return that station's information. I need to save that information(Name, Date and present Water Level) on regular basis obviously with C#.
I have some knowledge in C#. I have tried some codes on Webpage posting and Name Value Collection but yet not successful.
Codes are:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.yoursite.com");
request.Method = "POST";
formContent = "FormValue1=" + someValue +
"&FormValue2=" + someValue2 +
"&FormValue=" + someValue2;
byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = HttpUtility.UrlDecode(reader.ReadToEnd());
//You may need HttpUtility.HtmlDecode depending on the response
reader.Close();
dataStream.Close();
response.Close();
Another Code:
WebRequest req = WebRequest.Create("http://mysite/myform.aspx");
string postData = "item1=11111&item2=22222&Item3=33333";
byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;
Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
Can anyone help me in this regard?? It will be a great help for me.
Thanks in advance.
You can also read data through Jquery by their div or span value

reCaptcha validation time out

I am trying to validate Google reCaptcha on my 1and1 hosted website.
But when I execute this function on Server, It takes too long and my page is timed out.
Can anyone point me out what is causing problem.
private string VerifyRecaptcha(string secret, string responseCode)
{
WebRequest request = WebRequest.Create("https://www.google.com/recaptcha/api/siteverify");
request.Method = "POST";
string postData = "secret=" + secret + "&response=" + responseCode;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
This could be due to firewall issues on the server being able to communicate to the google domain. If you have a firewall, it may not be letting through calls to google, especially if it is filtering by IP address. Google has a range of IP's that you need to open up.

Can't get JSON POST response

I have this code:
WebRequest request = WebRequest.Create("https://getpocket.com/v3/oauth/request");
request.Proxy = WebRequest.DefaultWebProxy;
request.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Method = "POST";
string postData = "{\"consumer_key\":\"keyIsHere\",\"redirect_uri\":\"pickpocket:authorizationFinished\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
It returns code=12345456787blahblah instead of the JSON response: {"code":"12345456787blahblah"} and I can't figure out why. I got the POST request/response code from MSDN and the correct request data from the Pocket API
You need to add the X-Accept header:
request.Headers["X-Accept"] = "application/json";
From the API docs: "The X-Accept header indicates the format you would like to receive the response, the Pocket Authentication API supports two formats: application/x-www-form-urlencoded (DEFAULT) and application/json"

Getting error while fetching information from windows live server

I am trying to authenticate user through windows live account with following code.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
WebRequest request = WebRequest.Create("https://oauth.live.com/token");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
request.Method = "POST";
Stream resp = request.GetRequestStream();
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
var response = request.GetResponse();
But I am getting following error at last line.
The remote server returned an error: (400) Bad Request.
what should I do for this?
Your issue is probably because we do not send bytes on "application/x-www-form-urlencoded" post but a string. Also the GetRespose is not looks like the correct one. Your code must be something like:
// I do not know how you create the byteArray
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// but you need to send a string
string strRequest = Encoding.ASCII.GetString(byteArray);
WebRequest request = WebRequest.Create("https://oauth.live.com/token");
request.ContentType = "application/x-www-form-urlencoded";
// not the byte length, but the string
//request.ContentLength = byteArray.Length;
request.ContentLength = strRequest.Length;
request.Method = "POST";
using (StreamWriter streamOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII))
{
streamOut.Write(strRequest);
streamOut.Close();
}
string strResponse;
// get the response
using (StreamReader stIn = new StreamReader(request.GetResponse().GetResponseStream()))
{
strResponse = stIn.ReadToEnd();
stIn.Close();
}
// and here is the results
FullReturnLine = HttpContext.Current.Server.UrlDecode(strResponse);

Categories

Resources