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();
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 ?
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
I use HttpWebRequest to upload file on the server. But I also want to send some parameters (I mean name-value pairs)
You can add them to the query string. They'll be available on the server, regardless of whether the HTTP method is POST or GET.
you can try this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some site");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = "some data";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
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"
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);