I am trying to create a REST Client to measure the time taken for a HTTP Request to execute, that is to measure the time between the Client's request and the response from the server after it reaches the client.(There are other easier approaches to find this like fiddler etc, but I need this anyway). I am following Microsoft's example provided here:
https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx
should I just note the time when the response is returned?
// Send the request:
DateTime T = System.DateTime.UtcNow; //--> Note the initial Time
HttpWebResponse response = (HttpWebResponse) req.GetResponse();
TimeSpan TT = System.DateTime.UtcNow - T; //--> Note the Time Difference
or should I be calculating the time after the response stream is read:
DateTime T = System.DateTime.UtcNow;//--> Note the initial Time
HttpWebResponse response = (HttpWebResponse) req.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
TimeSpan TT = System.DateTime.UtcNow - T;//--> Note the Time Difference
I am just not sure about the exact lines where the Request is made to the server and the Response from the Server are available to the Client.
According to the msdn documentation, GetResponse() will send the request to the server and retreive the response.
If you're just interested in timing the call, then the first option would be your best bet.
Related
In my code, I am doing below
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
Stream dataStream = response.GetResponseStream();
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
httpWebRequest is a POST request whose response size in Fiddler after one redirection is 67722. response.ContentLength field returned in my case is -1 and total size of responseFromServer is approximately 32695. It simply cuts the remaining data from response. I have seen somewhere to use maxReceivedMessageSize but not able to figure our how to do it.
responseFromServer.Length 32695
response.ContentLength -1
response.GetResponseHeader("transfer-encoding") ""
UPDATE: FINAL
Response was coming right. Issue was with my code where parameters being sent into POST query were not encoded properly through UrlEncoding. If we are dealing with ASP.net contentpanel control, point to remember is that it sends the POST query with control names having special character '$'. We need to encode that using HttpUtility.UrlEncoding method
As mentioned by spender, check the response headers..the value for 'ContentLength' is supplied in the response headers, and may NOT be the actual length of data; if transfer-encoding is 'chunked', it is "normal" to mark contentlength as -1.
I'm not familiar with fiddler..but perhaps it is fetching things that the webresponse is not, like external scripts? Try loading the page in a normal webbrowser, and 'View Source' --> use some tool (A textbox..) to count the characters in the source: is it actually 67k or closer to 32? Similarly, check the last part of data in 'responseFromServer'..is it the same as when 'Viewing page source'? ie. both are probably (hopefully?) < / h t m l >
I am facing a weird problem with solr.
I am getting bad request(400) error when requesting a url with HttpWebRequest(C#)
We are using below code
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(solrFinalUrl);
HttpWebResponse response = ttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream,Encoding.UTF8);
String jsonString = readStream.ReadToEnd();
But when I am hitting the url on browser it's giving expected result.
Request Url --
http://__________:8080/solr-4.10.3/lemars/select?start=0&rows=10&q=*%3A*&fq=%20(date:[1887-08-01T00:00:00Z%20TO%201887-08-31T23:59:59Z])%20(date:[1888-08-01T00:00:00Z%20TO%201888-08-31T23:59:59Z])%20(date:[1889-08-01T00:00:00Z%20TO%201889-08-31T23:59:59Z])%20(date:[1890-08-01T00:00:00Z%20TO%201890-08-31T23:59:59Z])%20(date:[1891-08-01T00:00:00Z%20TO%201891-08-31T23:59:59Z])%20(date:[1892-08-01T00:00:00Z%20TO%201892-08-31T23:59:59Z])%20(date:[1893-08-01T00:00:00Z%20TO%201893-08-31T23:59:59Z])%20(date:[1894-08-01T00:00:00Z%20TO%201894-08-31T23:59:59Z])%20(date:[1895-08-01T00:00:00Z%20TO%201895-08-31T23:59:59Z])%20(date:[1896-08-01T00:00:00Z%20TO%201896-08-31T23:59:59Z])%20(date:[1897-08-01T00:00:00Z%20TO%201897-08-31T23:59:59Z])%20(date:[1898-08-01T00:00:00Z%20TO%201898-08-31T23:59:59Z])%20(date:[1899-08-01T00:00:00Z%20TO%201899-08-31T23:59:59Z])%20(date:[1900-08-01T00:00:00Z%20TO%201900-08-31T23:59:59Z])%20(date:[1901-08-01T00:00:00Z%20TO%201901-08-31T23:59:59Z])%20(date:[1902-08-01T00:00:00Z%20TO%201902-08-31T23:59:59Z])%20(date:[1903-08-01T00:00:00Z%20TO%201903-08-31T23:59:59Z])%20(date:[1904-08-01T00:00:00Z%20TO%201904-08-31T23:59:59Z])%20(date:[1905-08-01T00:00:00Z%20TO%201905-08-31T23:59:59Z])%20(date:[1906-08-01T00:00:00Z%20TO%201906-08-31T23:59:59Z])%20(date:[1907-08-01T00:00:00Z%20TO%201907-08-31T23:59:59Z])%20(date:[1908-08-01T00:00:00Z%20TO%201908-08-31T23:59:59Z])%20(date:[1909-08-01T00:00:00Z%20TO%201909-08-31T23:59:59Z])%20(date:[1910-08-01T00:00:00Z%20TO%201910-08-31T23:59:59Z])%20(date:[1911-08-01T00:00:00Z%20TO%201911-08-31T23:59:59Z])%20(date:[1912-08-01T00:00:00Z%20TO%201912-08-31T23:59:59Z])%20(date:[1913-08-01T00:00:00Z%20TO%201913-08-31T23:59:59Z])%20(date:[1914-08-01T00:00:00Z%20TO%201914-08-31T23:59:59Z])%20(date:[1915-08-01T00:00:00Z%20TO%201915-08-31T23:59:59Z])%20(date:[1916-08-01T00:00:00Z%20TO%201916-08-31T23:59:59Z])%20(date:[1917-08-01T00:00:00Z%20TO%201917-08-31T23:59:59Z])%20(date:[1918-08-01T00:00:00Z%20TO%201918-08-31T23:59:59Z])%20(date:[1919-08-01T00:00:00Z%20TO%201919-08-31T23:59:59Z])%20(date:[1920-08-01T00:00:00Z%20TO%201920-08-31T23:59:59Z])%20(date:[1921-08-01T00:00:00Z%20TO%201921-08-31T23:59:59Z])%20(date:[1922-08-01T00:00:00Z%20TO%201922-08-31T23:59:59Z])%20(date:[1923-08-01T00:00:00Z%20TO%201923-08-31T23:59:59Z])%20(date:[1924-08-01T00:00:00Z%20TO%201924-08-31T23:59:59Z])%20(date:[1925-08-01T00:00:00Z%20TO%201925-08-31T23:59:59Z])%20(date:[1926-08-01T00:00:00Z%20TO%201926-08-31T23:59:59Z])%20(date:[1927-08-01T00:00:00Z%20TO%201927-08-31T23:59:59Z])%20(date:[1928-08-01T00:00:00Z%20TO%201928-08-31T23:59:59Z])%20(date:[1929-08-01T00:00:00Z%20TO%201929-08-31T23:59:59Z])%20(date:[1930-08-01T00:00:00Z%20TO%201930-08-31T23:59:59Z])%20(date:[1931-08-01T00:00:00Z%20TO%201931-08-31T23:59:59Z])%20(date:[1932-08-01T00:00:00Z%20TO%201932-08-31T23:59:59Z])%20(date:[1933-08-01T00:00:00Z%20TO%201933-08-31T23:59:59Z])%20(date:[1934-08-01T00:00:00Z%20TO%201934-08-31T23:59:59Z])%20(date:[1935-08-01T00:00:00Z%20TO%201935-08-31T23:59:59Z])%20(date:[1936-08-01T00:00:00Z%20TO%201936-08-31T23:59:59Z])%20(date:[1937-08-01T00:00:00Z%20TO%201937-08-31T23:59:59Z])%20(date:[1938-08-01T00:00:00Z%20TO%201938-08-31T23:59:59Z])%20(date:[1939-08-01T00:00:00Z%20TO%201939-08-31T23:59:59Z])%20(date:[1940-08-01T00:00:00Z%20TO%201940-08-31T23:59:59Z])%20(date:[1941-08-01T00:00:00Z%20TO%201941-08-31T23:59:59Z])%20(date:[1942-08-01T00:00:00Z%20TO%201942-08-31T23:59:59Z])%20(date:[1943-08-01T00:00:00Z%20TO%201943-08-31T23:59:59Z])%20(date:[1944-08-01T00:00:00Z%20TO%201944-08-31T23:59:59Z])%20(date:[1945-08-01T00:00:00Z%20TO%201945-08-31T23:59:59Z])%20(date:[1946-08-01T00:00:00Z%20TO%201946-08-31T23:59:59Z])%20(date:[1947-08-01T00:00:00Z%20TO%201947-08-31T23:59:59Z])%20(date:[1948-08-01T00:00:00Z%20TO%201948-08-31T23:59:59Z])%20(date:[1949-08-01T00:00:00Z%20TO%201949-08-31T23:59:59Z])%20(date:[1950-08-01T00:00:00Z%20TO%201950-08-31T23:59:59Z])%20(date:[1951-08-01T00:00:00Z%20TO%201951-08-31T23:59:59Z])%20(date:[1952-08-01T00:00:00Z%20TO%201952-08-31T23:59:59Z])%20(date:[1953-08-01T00:00:00Z%20TO%201953-08-31T23:59:59Z])%20(date:[1954-08-01T00:00:00Z%20TO%201954-08-31T23:59:59Z])%20(date:[1955-08-01T00:00:00Z%20TO%201955-08-31T23:59:59Z])%20(date:[1956-08-01T00:00:00Z%20TO%201956-08-31T23:59:59Z])%20(date:[1957-08-01T00:00:00Z%20TO%201957-08-31T23:59:59Z])%20(date:[1958-08-01T00:00:00Z%20TO%201958-08-31T23:59:59Z])%20(date:[1959-08-01T00:00:00Z%20TO%201959-08-31T23:59:59Z])%20(date:[1960-08-01T00:00:00Z%20TO%201960-08-31T23:59:59Z])%20(date:[1961-08-01T00:00:00Z%20TO%201961-08-31T23:59:59Z])%20(date:[1962-08-01T00:00:00Z%20TO%201962-08-31T23:59:59Z])%20(date:[1963-08-01T00:00:00Z%20TO%201963-08-31T23:59:59Z])%20(date:[1964-08-01T00:00:00Z%20TO%201964-08-31T23:59:59Z])%20(date:[1966-08-01T00:00:00Z%20TO%201966-08-31T23:59:59Z])%20(date:[1967-08-01T00:00:00Z%20TO%201967-08-31T23:59:59Z])%20(date:[1968-08-01T00:00:00Z%20TO%201968-08-31T23:59:59Z])%20(date:[1969-08-01T00:00:00Z%20TO%201969-08-31T23:59:59Z])%20(date:[1970-08-01T00:00:00Z%20TO%201970-08-31T23:59:59Z])%20(date:[1971-08-01T00:00:00Z%20TO%201971-08-31T23:59:59Z])%20(date:[1972-08-01T00:00:00Z%20TO%201972-08-31T23:59:59Z])%20(date:[1973-08-01T00:00:00Z%20TO%201973-08-31T23:59:59Z])%20(date:[1974-08-01T00:00:00Z%20TO%201974-08-31T23:59:59Z])%20(date:[1975-08-01T00:00:00Z%20TO%201975-08-31T23:59:59Z])%20(date:[1976-08-01T00:00:00Z%20TO%201976-08-31T23:59:59Z])%20(date:[1977-08-01T00:00:00Z%20TO%201977-08-31T23:59:59Z])%20(date:[1978-08-01T00:00:00Z%20TO%201978-08-31T23:59:59Z])%20(date:[1979-08-01T00:00:00Z%20TO%201979-08-31T23:59:59Z])%20(date:[1980-08-01T00:00:00Z%20TO%201980-08-31T23:59:59Z])%20(date:[1981-08-01T00:00:00Z%20TO%201981-08-31T23:59:59Z])%20(date:[1982-08-01T00:00:00Z%20TO%201982-08-31T23:59:59Z])%20(date:[1983-08-01T00:00:00Z%20TO%201983-08-31T23:59:59Z])%20(date:[1984-08-01T00:00:00Z%20TO%201984-08-31T23:59:59Z])%20(date:[1985-08-01T00:00:00Z%20TO%201985-08-31T23:59:59Z])%20(date:[1986-08-01T00:00:00Z%20TO%201986-08-31T23:59:59Z])%20(date:[1987-08-01T00:00:00Z%20TO%201987-08-31T23:59:59Z])%20(date:[1988-08-01T00:00:00Z%20TO%201988-08-31T23:59:59Z])%20(date:[1989-08-01T00:00:00Z%20TO%201989-08-31T23:59:59Z])%20(date:[1990-08-01T00:00:00Z%20TO%201990-08-31T23:59:59Z])%20(date:[1991-08-01T00:00:00Z%20TO%201991-08-31T23:59:59Z])%20(date:[1992-08-01T00:00:00Z%20TO%201992-08-31T23:59:59Z])%20(date:[1993-08-01T00:00:00Z%20TO%201993-08-31T23:59:59Z])%20(date:[1994-08-01T00:00:00Z%20TO%201994-08-31T23:59:59Z])%20(date:[1995-08-01T00:00:00Z%20TO%201995-08-31T23:59:59Z])%20(date:[1996-08-01T00:00:00Z%20TO%201996-08-31T23:59:59Z])%20(date:[1997-08-01T00:00:00Z%20TO%201997-08-31T23:59:59Z])%20(date:[1998-08-01T00:00:00Z%20TO%201998-08-31T23:59:59Z])%20(date:[1999-08-01T00:00:00Z%20TO%201999-08-31T23:59:59Z])%20(date:[2000-08-01T00:00:00Z%20TO%202000-08-31T23:59:59Z])%20(date:[2001-08-01T00:00:00Z%20TO%202001-08-31T23:59:59Z])%20(date:[2002-08-01T00:00:00Z%20TO%202002-08-31T23:59:59Z])%20(date:[2003-08-01T00:00:00Z%20TO%202003-08-31T23:59:59Z])%20(date:[2004-08-01T00:00:00Z%20TO%202004-08-31T23:59:59Z])%20(date:[2005-08-01T00:00:00Z%20TO%202005-08-31T23:59:59Z])%20(date:[2006-08-01T00:00:00Z%20TO%202006-08-31T23:59:59Z])%20(date:[2007-08-01T00:00:00Z%20TO%202007-08-31T23:59:59Z])%20(date:[2008-08-01T00:00:00Z%20TO%202008-08-31T23:59:59Z])%20(date:[2009-08-01T00:00:00Z%20TO%202009-08-31T23:59:59Z])%20(date:[2010-08-01T00:00:00Z%20TO%202010-08-31T23:59:59Z])%20(date:[2011-08-01T00:00:00Z%20TO%202011-08-31T23:59:59Z])%20(date:[2012-08-01T00:00:00Z%20TO%202012-08-31T23:59:59Z])&fl=filename%2Ctitle%2CfileContent%2Cdate%2Ccity%2Cstate%2Cpagenum&wt=json&indent=true&facet=true&facet.range=date&facet.range.start=1887-01-01T00:00:00Z/MONTH&facet.range.end=2012-12-31T23:59:59Z/MONTH%2B1MONTH&facet.range.gap=%2B1DAY
Please help.
You can analyze http packages by wireshark. Compare if there are any different between connect from c# application and browser.
I am checking out the namecheap api and am having some difficulty getting started. I am trying to access the api after setting up the sandbox account etc and a sample response is in XML format:
<ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
<Errors />
<Warnings />
<RequestedCommand>namecheap.domains.check</RequestedCommand>
<CommandResponse>
<DomainCheckResult Domain="google.com" Available="false" />
</CommandResponse>
<Server>WEB1-SANDBOX1</Server>
<GMTTimeDifference>--4:00</GMTTimeDifference>
<ExecutionTime>0.875</ExecutionTime>
</ApiResponse>
I know how to parse XML, but what I need a little guidance with is how do I get started with the actual request/response part of the API call.
I know which parameters I need to send, and I know I need the api key and url, but how do I write the WebRequest and WebResponse part of it? Or can Linq provide me a way to achieve that too?
I was trying to use:
WebRequest req = HttpWebRequest.Create(url + apikey + username + command + domain);
WebResponse response = req.GetResponse();
But I don't see a way to do anything with variable response.
How can I make a very simple API call to this API and get its response into XML format so I can parse it?
Any help at all is really appreciated.
You need to get the Response Stream associated and read from it:
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
Console.WriteLine ("Response stream received.");
Console.WriteLine (readStream.ReadToEnd ());
WebResponse is abstract, you must cast it to an HttpWebResponse.
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
You will then be able to access the various information inside that you're looking for.
Also, you might consider using a WebClient if you're just doing simple web requests, it's much easier to work with... literally as easy as:
string response = new WebClient().DownloadString("http://somewebsite.com/some/resource?params=123");
I'm trying to integrate with Shopping.com REST service.
so I wrote a simple (minimum code) C# application to retrieve the XML data using HttpWebRequest class. I've used StopWatch for bench-marking and the response time (Including Stream.ReadToEnd()) is something like 1300-1700 milliseconds.
it might sound good, but then I've tested the response time in Chrome browser with Fiddler and the response time was about 600-800.
I've read few articles. some suggested set HttpWebRequest.Proxy to null / WebRequest.DefaultWebProxy but it didn't make significant improvement.
Here the request-url:
http://sandbox.api.shopping.com/publisher/3.0/rest/GeneralSearch?apiKey=78b0db8a-0ee1-4939-a2f9-d3cd95ec0fcc&trackingId=7000610&keyword=nikon
So, what should I do to reach this response time?
Are you enabling gzip and deflate?
Also, the first time you execute a web request in your code, there is a warmup to load the necessary assemblies, initialize the service point, and establish the HTTP connection, so be sure to look at the time for the second and subsequent executions.
EDIT: Sorry you will still need to decode the results using a GZipStream or DeflateStream, but this at least gives you the idea. See here for more info: HttpWebRequest & Native GZip Compression
var timer = Stopwatch.StartNew();
var url = "http://sandbox.api.shopping.com/publisher/3.0/rest/GeneralSearch?apiKey=78b0db8a-0ee1-4939-a2f9-d3cd95ec0fcc&trackingId=7000610&keyword=nikon";
var webRequest = WebRequest.Create(url);
webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
using (var webResponse = webRequest.GetResponse())
using (var responseStream = webResponse.GetResponseStream())
using (var streamReader = new StreamReader(responseStream))
{
var content = streamReader.ReadToEnd();
}
var timeSpan = timer.Elapsed;
Console.WriteLine(timeSpan);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
using (WebResponse response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
var result = reader.ReadToEnd();
// Do something with result
}
In the above example I have a timeout defined, if it happens to hit the timeout how would I know, the result would be empty ?
Do I receive any reponse types ?
How can I make sure I got timed out ?
GetResponse() would throw a WebException. It's simple to test exactly what happens though - set the timeout to 1ms and try to hit anything which takes a while to come back.
In fact, the documentation states this explicitly:
If the timeout period expires before the resource can be returned, a WebException is thrown.
Your HttpWebRequest.GetResponse call will throw a WebException when;
Abort was previously called.
-or-
The time-out period for the request expired.
-or-
An error occurred while processing the request.
Catch this exception.
I used to just pull my network cable out to test this sort of thing although you could be more elegant and use a proxy tool and block that particular request.
You should probably be using HTTPWebResponse. It has a status code that tells you that information and more. HTTPWebResponse is a descendant of WebResponse.