For POST requests using HttpWebRequest, when I write to a request stream, at what point does the data get sent? Is it when I close the request stream or when I call GetResponse? Is the GetResponse call required?
The .net documentation does not seem to be very clear about what is really happening
Here's the code I'm curious about:
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentLength = jsonData.Length;
request.ContentType = "application/json";
Stream requestStream = request.GetRequestStream();
requestStream.Write(jsonData, 0, jsonData.Length);
requestStream.Close();
var response = request.GetResponse() as HttpWebResponse;
Thanks!
Yes, GetResponse call is must, not only for POST request but for GET, HEAD requests too. Request / data is sent at the point when you call GetResponse.
Start the sniffer and set breakpoint on your requestStream.Close(); and you will see that request is making when GetResponse() called.
Related
I have class in c# which makes POST call to a REST API. BUt it gives me a 'connection time out' error with error code as 10060. In the POST call I am trying to make some transaction adjustments in the client's system. When I use Fiddler or Postman to make the api call, the request seems to go through but not from the c# class. Can you see where I am going wrong?
Below is my sample code.
HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp(clientURL);
byte[] data = Encoding.UTF8.GetBytes(urlParameters);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream()) **//exception occurs at this point**
{
stream.Write(data, 0, data.Length);
}
Thanks.
You have to close your webrequest by using "using" or adding a call to the HttpWebRequest.Abort() method
The default timeout for HttpWebRequest is 100 seconds. You can change that value by setting that property (in ms):
request.Timeout = 120000;
how would I be able to do something like http://myanimelist.net/modules.php?go=api#verifycred
aka "curl -u user:password http://myanimelist.net/api/account/verify_credentials.xml"
I wish to option the id
my code so far is
string url = "http://myanimelist.net/api/account/verify_credentials.xml";
WebRequest request = WebRequest.Create(url);
request.ContentType = "xml";
request.Method = "GET";
request.Credentials = new NetworkCredential(username, password);
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("xml/user/id"); // i think this line?
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
but I get a error on "reqstr.Write(buffer, 0, buffer.Length)"
Cannot send a content-body with this verb-type, I have tried googling but with no prevail, I am using c#
Your snippet is trying to send a GET request with request data (you're calling GetRequestStream and then writing some data to the request stream). The HTTP protocol does not allow this - you can only send data with POST request.
However, the API that you are trying to call is actually doing something different - you do not need to send it the XML data. The XML data (with user ID and user name) is the response that you get when you successfully login.
So, instead of calling GetRequestStream and writing the XML data, you need to call GetResponse and then GetResponseStream to read the XML data!
I need to post data to a website. So I created a small app in C#.net where I open this website and fill in all the controls (radio buttons, text boxes, checkboxes etc) with the values from my database. I also have a click event on the SUBMIT button. The app then waits for 10-15 seconds and then copies the response from the webpage to my database.
As you can see, this is really a hectic process. If there are thousands of records to upload, this app takes much longer (due to fact that it waits 15s for the response).
Is there any other way to post data? I am looking for something like concatenating all the fields with its value and uploading it like a stream of data. How will this work if the website is https and not http?
You can use HttpWebRequest to do this, and you can concatenate all the values you want to post into a single string for the request. It could look something like this:
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();
This method should work fine for http and https.
MSDN has a great article with step-by-step instructions detailing how you can use the WebRequest class to send data. Link below:
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
Yes, there is a WebClient class. Look into documentation. There're some usful method to make GET and POST requests.
I am requesting a handler file from another handler file that returns an image, when I request my HttpWebRequest it takes more time to get the response. Here is my code, please help.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpCookie cookie = context.Request.Cookies["ASP.NET_SessionID"];
Cookie myCookie = new Cookie(cookie.Name, cookie.Value);
myCookie.Domain = url.Host;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(myCookie);
request.Timeout = 200000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
First make sure that your request is getting to the handler as quickly as possible. If not then it is some issue with your network. You can diagnose this with logs or debugging or whatever way you need. Use Fiddler to re-issue requests so you know exactly when it is fired and received.
If it is getting to the server and no processing on there is taking too much time then make sure that you are flushing and closing the response stream when you are finished writing to it. Also probably best to dispose of the responseStream object.
using(var responseStream = response.GetResponseStream()){
// write to the sucker
responseStream.Flush();
responseStream.Close();
}
NB If this is only on the first request (your question/answers are a little confusing) then try work out what exactly is happening at startup of the appdomain. Is there something big in global.asax - or is it doing a lot of DB work?
I have a silverlight application which calls a web page, on clikc of a button. I want to send some parameters to the page on click of the button.
Now I can send it via query string but I don't want to do it as I might want to send a list of users which can go lenghty.
Also using sessions is not an option as these are two different applications. Also on click of the button we have to do some operations and display the results in the web page.
Is there a way I can call the web method in the page - Do my operation and then then show the details of the operation on my web page.
Maybe you could use something like the WebRequest class to create a POST request and then use the HttpWebResponse to get the details.
var request = (HttpWebRequest) WebRequest.Create(Uri);
request.Method = "POST";
var postData = string.Format("param1={0}¶m2={1}", "value1", "value2");
var data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
var response = request.GetResponse() as HttpWebResponse;