http://i.stack.imgur.com/hDpwl.jpg
I need a code snipped which I can use in a WPF application. It should send a post request to the server and return a JSON. Best would be if its working same as the print screen from chrome add-on postman above. I tried to use uploading form Data with HttpClient and uploading form data with WebRequest and it returned HTML page with an error.
Thanks all
public string PostJson(string url,string json)
{
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
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();
Console.Writeline(string.Format("Request {0} Response Code {1} Resonse Status Description {2} Response From Server {3}",
json, ((HttpWebResponse)response).StatusCode,
((HttpWebResponse)response).StatusDescription, responseFromServer));
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
Related
I use HttpRequest object to get data from a webapi which repose Jsonp data.But the responseFromServer object is empty although Chrome Network shows it indeed repose data back.Here is My Code(c#).Greatly appreciated.
WebRequest request = WebRequest.Create(url);
request.Method = "post";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/json";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
var status = ((HttpWebResponse)response).StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
I need to implement an application to post string like HttpRequest to specify IP address and then receive answer in httpRespons form.
Here is code for sending
WebRequest request = WebRequest.Create("http://192.168.1.10");
request.Method = "POST";
string postData = "string I want to post";
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();
I'm not able to test this code. Is this code OK?
Here is code for receive answer:
WebResponse response = request.GetResponse();
richTextBox1.AppendText(((HttpWebResponse)response).StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
richTextBox1.AppendText(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
Is this code OK?
The code is correct,but i think StreamReader reader = new StreamReader(dataStream); should append Encoding :StreamReader reader = new StreamReader(dataStream,encoding);
By using web request/response classes, i ma trying to login into Pinterest website.. But i am not able to do that as it is giving me 'csrf error'.
Below is my code.
WebRequest request = WebRequest.Create("https://www.pinterest.com/login");
request.Credentials = new NetworkCredential("myid", "mypassword");
request.Method = "POST";
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = System.Text.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();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
Console.ReadLine();
reader.Close();
dataStream.Close();
response.Close();
what i am doing wrong here ?
I am trying to login a website with c# , I succedd to login with sending username and password, I also opened auto redirecting property of request but web server needs 2 redirection in order to enter welcome page but in my code it can only redirects once. How can I get final directed page? Any help would be appreciated.
HttpWebRequest request =(HttpWebRequest)WebRequest.Create("http://website.com/client/patronlogin.loginpageform/DEFAULT");
request.AllowAutoRedirect = true; // IMPORTANT
request.Method = "POST";
int pass = sfsdf1;
int no = dsfsdfdf0;
String formContent = "t%3Aac=%24002f%24002ftreblead.com%24002fclient%24002fdefault%24002fsearch%24002faccount%24003f&t%3Aformdata=H4sIAAAAAAAAAJWQv0oDQRDGx4NAMJ1gEURstN2zMI02BkEQDgkc1mFvb7xs2Ntdd%2FZMbKx8CRufQKz0CVLY%2BQ4%2BgI2FlYV7J6Lg%2F274mJnv932XD9CarMAyIXdiFA%2B4d0YnppB6czysCJ3mJZKDnnEF45aLETLPLZJ3Jz0mjEMlM5ZxQtbPgsiF35Wo8tUUfWXXDmad%2B8Xb5wjmEugIo8N3tR8%2BelhIxvyYx4rrIk69k7rYmloP8%2B%2Buf8Hq%2Fxdr4IxAorTKSkkkjZ5d5RuHTxd3EcDUfmtpOdHEuJyO4BSgwXyTfr2pT1qTJeh%2BsUU1hw9Btn8MIkxpjUbtiTXk%2FnOO8%2FSxe3N9thNBlEBbKBm29xrvunpUWAahrr6R6qrbr%2BbD9Q%2FjCx9ggTUPAgAA&j_username="+no.ToString()+"&j_password="+pass.ToString();
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();
// Get the response ...
WebResponse response;
response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
richTextBox1.AppendText(reader.ReadToEnd());
richTextBox1.AppendText(HttpUtility.UrlDecode(reader.ReadToEnd()));
reader.Close();
dataStream.Close();
response.Close();
Could it be that you need to set MaximumAutomaticRedirections property to a higher value?
I want to reuse a WebRequest object so that cookies and session would be saved for later request to the server. Below is my code. If i use Post function twice on the second time at
request.ContentLength = byteArray.Length;
it will throw an exception
This property cannot be set after writing has started!
But as you can see
dataStream.Close();
Should close the writing process! Anybody knows what's going on?
static WebRequest request;
public MainForm()
{
request = WebRequest.Create("http://localhost/admin/admin.php");
}
static string Post(string url, string data)
{
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(data);
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();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
request.Abort();
return responseFromServer;
}
You cannot reuse the WebRequest. Take the returned HttpWebResponse.Cookies, construct a new WebRequest and fill the HttpWebRequest.CookieContainer. (You need to assign a CookieContainer in the first request to get Cookies returned.)