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();
Related
I am writing http post method which should have request header with next values
authkey : "somevalue",
number : "somenumber",
entity : "someentity".
Also customer ask me to upload xml file as form data. I am not sure I know how to do this.
The following code shows what I have implemented for now:
var req = new HttpRequestMessage(HttpMethod.Post, destinationUrl);
req.Headers.Add("authKey", "somevalue");
req.Headers.Add("number", somenumber);
req.Headers.Add("entity", "someentity");
How can I add my XML? I have found the following code already but not sure it can work for this case :
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(serverResponse);
req.ContentType = "text/xml; encoding='utf-8'";
req.ContentLength = bytes.Length;
req.Method = "POST";
Stream requestStream = req.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse resp;
This looks to have been answered before here... https://stackoverflow.com/a/17535912/3585339
The part you are missing is reading the response into the resp variable you have created like this:
resp = (HttpWebResponse)request.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = resp.GetResponseStream();
string responseString = new StreamReader(responseStream).ReadToEnd();
return responseString;
}
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 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"
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);