HttpRequest.GetResponse() taking too much time - c#

I have an outlook addin application which I'm connecting with Salesforce.
I'm trying to request some data from SF API which normally returns result in less than 1 second, but with my code It takes from 5-15 seconds to complete.
I have also tried to set proxy to null.
Here is my code:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
if (includeCustomHeader == true)
{
req.Headers.Add("Authorization: OAuth " + ServiceManager.Token.access_token);
req.Headers.Add("X-PrettyPrint:1");
req.ContentType = "application/json";
}
else
{
req.ContentType = "application/x-www-form-urlencoded";
}
req.Method = "POST";
req.Proxy = null;
byte[] data = System.Text.Encoding.ASCII.GetBytes(payload);
req.ContentLength = data.Length;
using (var responseStream = req.GetRequestStream())
{
responseStream.Write(data, 0, data.Length);
}
//here it's taking from 5-15 seconds, each request gets a batch of 200 records
using (var response = req.GetResponse())
{
return new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
}
Don't know what am I missing , or could there be any other reason?
Suggestions?

Related

HttpWebRequest.GetResponse() timeout issue

I get intermittent timeout issue with getting a response from HttpWebRequest.GetResponse(). I thought it was an issue with the server, but the server seems to have sent the response within a minute. I have set timeout to be 20 mins.
GetResponse() keeps on waiting and then throws:
The operation has timed out.
I have also used GetRequestStream(), GetResponse() in using blocks. So, I don't think that could be an issue. The issue seems very sporadic. Any help would be appreciated.
var request = WebRequest.CreateHttp("http://someurl")
request.Method = "POST";
request.ContentLength = data.Length;
request.ContentType = "text/xml; encoding='utf-8'";
request.KeepAlive = false;
request.Timeout = 1200 * 1000;
request.ServicePoint.ConnectionLeaseTimeout = request.Timeout;
request.ServicePoint.MaxIdleTime = request.Timeout;
using (System.IO.Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (System.Net.WebResponse response = request.GetResponse())
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
{
var response = reader.ReadToEnd();
}
}

Method Not Allowed using Cisco REST API

I am trying to change agent state using REST API provided from Cisco.
here is the code that I wrote :
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://url:8445/finesse/api/User/agent2"));
request.Credentials = new NetworkCredential("agent2", "12345");
request.Method = "POST";
request.ContentType = "application/xml";
// request.Accept = "application/xml";
XElement redmineRequestXML =
new XElement("User",
new XElement("state", "READY"),
new XElement("extension", "3010")
);
byte[] bytes = Encoding.UTF8.GetBytes(redmineRequestXML.ToString());
request.ContentLength = bytes.Length;
using (Stream putStream = request.GetRequestStream())
{
putStream.Write(bytes, 0, bytes.Length);
}
// Log the response from Redmine RESTful service
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
MessageBox.Show(reader.ReadToEnd());
}
And I am getting this error :
The remote server returned an error: (405) Method Not Allowed.
so please any idea could help solving this issue ?
after 2 days I figured out that the issue is to replace this line :
request.Method = "POST";
by this line :
request.Method = "PUT";

HttpWebRequest get the same response object when it's called in the second time

I am using HttpWebRequest to get webpage source code with POST method.
The page needs to be accessed for multiple times with different parameters.
When getting response in the second time in a short period, it always return the same response object to me. After debugging, if the second call is after around 30 seconds, the request can get the correct response object(source code).
public HtmlAgilityPack.HtmlDocument getHtmlData(string url, string cname)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
try
{
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
HttpWebRequest.DefaultCachePolicy = policy;
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.KeepAlive = false;
req.Headers[HttpRequestHeader.CacheControl] = "no-cache";
req.Headers[HttpRequestHeader.Pragma] = "no-cache";
req.IfModifiedSince = DateTime.Now;
req.CachePolicy = noCachePolicy;
//add post data
string postData = "cname=" + cname;
byte[] byteArray = Encoding.GetEncoding("shift-jis").GetBytes(postData);
req.ContentLength = byteArray.Length;
using (Stream stream = req.GetRequestStream())
{
stream.Write(byteArray, 0, byteArray.Length);
}
// get response
string data = "";
HttpWebResponse response = req.GetResponse() as HttpWebResponse;
using (var stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("shift-jis"));
data = reader.ReadToEnd();
reader.Close();
}
response.Close();
doc.LoadHtml(data);
//System.Threading.Thread.Sleep(30000);
}
catch(Exception ex)
{
Log.log.Debug(ex.ToString());
}
return doc;
}
Function call block:
getHtmlData(#"http://www.jra.go.jp/JRADB/accessS.html", "pw01sli00/AF");
getHtmlData(#"http://www.jra.go.jp/JRADB/accessS.html", "pw01skl00999999/B3");
getHtmlData(#"http://www.jra.go.jp/JRADB/accessS.html", "pw01skl00201604/E3");
I have stuck with the problem for a whole day. hope someone can give me a clue.
Many thanks!

Long running, but finite length HttpWebRequest Callout gets only partial data

I am trying to work with a 3rd party service api . One of the methods they have brings in all records which takes a lot of time , about 9 mins ( i tried this using chrome app "Advanced Rest Client").
I have tried setting the webRequest.Timeout = 3600000;// Timeout.Infinite;
But it always comes back after maybe 2 minutes and the result contains 135 records (whereas the chrome app gets back 1050 records, which is correct #)
I am using the same parameters in both cases (i send it as POST data); so why is there difference in the results ?
I am using this code in a class library which will be used in a WPF application.
Is there a max value for timeout ?
Timeout.Infinite is setting it to '-1' , is this right ?
What are the other workarounds to get all data ?
Any help/suggestion is greatly appreciated.
Update: Adding code
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
AllDevicesList devInfo = null;
try
{
string postData = "";
foreach (string key in postParameters.Keys)
{
postData += HttpUtility.UrlEncode(key) + "="
+ HttpUtility.UrlEncode(postParameters[key]) + "&";
}
postData = postData.TrimEnd('&');
if (cookie == null)
webRequest.CookieContainer = new CookieContainer();
else
webRequest.CookieContainer = cookie;
webRequest.Timeout = 3600000;// Timeout.Infinite; // 1000000;
webRequest.KeepAlive = true;
webRequest.Method = "POST";
byte[] data = Encoding.ASCII.GetBytes(postData);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = data.Length;
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
WebResponse WebResp = webRequest.GetResponse();
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(AllDevicesList));
object objResponse = jsonSerializer.ReadObject(WebResp.GetResponseStream());
devInfo = objResponse as AllDevicesList;
requestStream.Close();
WebResp.Close();
I have changed the implementation to Asynchronous and now i get all the records.
Thanks for all your responses.

C# Webrequest to Sockets

I have an application that starts multiple threads, and each thread sends requests to a remote server. After profiling, I noticed that the most time is taken in sending & processing the request. This is the function:
private string GetRequest(string url, string postFields)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = proxy;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Host = Constants.URLDomain;
request.KeepAlive = true;
request.Accept = Constants.Accept;
request.ContentType = "application/json";
request.Headers.Add("MyHeader1", "true");
request.Headers.Add("MyHeader2", "header2value");
request.Headers.Add("MyHeader3", "header3value");
request.Method = "POST";
byte[] postBytes = Encoding.UTF8.GetBytes(postFields);
request.ContentLength = postBytes.Length;
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(postBytes, 0, postBytes.Length);
}
string text = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream answer = response.GetResponseStream())
using (StreamReader reader = new StreamReader(answer, System.Text.Encoding.UTF8))
{
text = reader.ReadToEnd();
}
}
return text;
}
I was thinking of changing this code to use sockets so I could improve performance. Will there be actually an improvement if I change to sockets, and more importantly, how do will a sockets implementation of the above code look like?

Categories

Resources