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"
Related
I have code:
var request =
(HttpWebRequest)WebRequest.Create("http://www.dba.dk/ajax/vip/telephonenumber/gettelephonenumber/?externalid=1033601271");
string stringData = "externalid=1033601271";
var data = Encoding.ASCII.GetBytes(stringData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = data.Length;
var newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
WebResponse response = request.GetResponse();
MessageBox.Show(((HttpWebResponse)response).StatusDescription);
response.Close();
With this I recieving 400 Bad Request from server. I think that I need to define Request Body: - I checked on firefox Network Analyzer it should be something like this: externalId=1033601271&__RequestVerificationToken=Xd5coguwXJArf6-5mQNl8eXOoBpUEltnpq2SvVfLSfo6Fe5MAw4VoaWT89NPEFJPONppjePDF5mWVO1CzMbGqqWA1KS2M8ZXmpJ0DNExcSCrxCIPPF_pBjP7lkRbt-rs9HrpHQ2
How I can define it in my code ? Also How I can read response from server to string variable ?
Box.com's Enterprise User Provisioning API requires OAUTH2 token in the header of the request ("Authorization: Bearer faKE_toKEN_1234"). I've ran the code below against http://www.xhaus.com/headers, http://httpbin.org/post and http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi and observed packets with Microsoft Network Monitor and as far as I know my request header does not include the "Authorization" value I wish to include there.
Is the code below missing something (code or a point)?
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(API_URL);
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout=10000;
string postData = Parameters;
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);
request.ContentLength = byte1.Length;
Stream reqStream = request.GetRequestStream();
reqStream.Write(byte1, 0, byte1.Length);
reqStream.Close();
//This is puzzling me, why can't I see this header anywere
//when debugging with packet monitor etc?
request.Headers.Add("Authorization: Bearer " + access_token);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string txtResponse = reader.ReadToEnd ();
return txtResponse;
I think you need to set the header before you write the postData and close the request stream. This appeared to work for me:
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.xhaus.com/headers");
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;
request.Headers.Add("Authorization: Bearer_faKE_toKEN_1234");
string postData = "postData";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postData);
request.ContentLength = byte1.Length;
Stream reqStream = request.GetRequestStream();
reqStream.Write(byte1, 0, byte1.Length);
reqStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string txtResponse = reader.ReadToEnd();
Console.WriteLine(txtResponse);
Console.ReadKey();
}
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
My Problem is that I have to integrate payment gateway. My request and response fine with Payment Gateway. But problem of this gateway is it only communicate with public server. Now I want to debug my later code which depend on response of payment Gateway.
Current scenario is,
A page --> payment Gateway --> B page
Now I know that response of payment gateway is fine so I want just send hardcoded response of payment gateway from A Page --> B Page by help of HttpWebResponse with POST Method,
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(redirectUrl);
request.Method = "POST";
string formContent = "data";
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();
But it not send post data to my B Page so that I can debug my B Page
Try this code.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(redirectUrl);
request.Method = "POST";
string formContent = "data";
byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(byteArray);
requestWriter.Close();
dataStream.Close();
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);