How to Resolve "The operation has timed out" error - c#

I have a problem that while downloading the data it shows the error "The Operation has timed out".
What can i do to resolve this error? I am using Win forms(C#) here is my code please check it and give suggestions. Where should i change the code please help me...
public void ProcessData()
{
try
{
string MessageTitle = "";
int pages = Convert.ToInt32(txtPages.Text);
for (int k = Count; k <= pages; k++)
{
string url = "http://www.yellowpages.com/" +StateName.ToLower()+ "/" + CategoryName + "?g=" + StateName + "&page=" + k + "&q=" + CategoryName + "";//txtYP.Text + k;
System.Net.HttpWebRequest httpRequest;
System.Net.HttpWebResponse httpResponse;
System.IO.StreamReader SReader;
string html;
httpRequest = (System.Net.HttpWebRequest)(System.Net.HttpWebRequest.Create(url));
httpRequest.Method = "GET";
httpResponse = (System.Net.HttpWebResponse)(httpRequest.GetResponse());
SReader = new StreamReader(httpResponse.GetResponseStream());
html = SReader.ReadToEnd();
string strDummy = html;
httpResponse.Close();

How long is it before the request times out?
Are you able to navigate to the url from a web browser?
Set HttpWebRequest.ReadWriteTimeout property on HttpWebRequest to a much higher value than what it is currently. The default value is 5 minutes.
Not sure why it should take more than 5 minutes.
Instead of blocking on the getresponse, you could as well use async callbacks (BeginGetResponse/EndGetResponse).
EDIT
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.HttpListener">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Cache">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add
name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace.log"
traceOutputOptions = "ProcessId, DateTime"
/>
</sharedListeners>
<switches>
<add name="System.Net"
value="Verbose" />
<add name="System.Net.Sockets"
value="Verbose" />
<add name="System.Net.Cache"
value="Verbose" />
<add name="System.Net.HttpListener"
value="Verbose" />
</switches>
</system.diagnostics>
Add this section inside configuration section in the app.config of your application.
After adding the above, rebuild the solution and run it.
Look at the trace.log written in the bin directory of your application for more details.

add this to your code :
httpRequest.Timeout = 3600000;
this will increase the request timeout to one hour.

I have face same issue while executing my console application on server. Below solution work for me :
Uncheck the automatic configuration script under Lan settings in internet option and check the automatic detect settings.
It resolve my problem for operation time out error .

Related

logging services calls in .txt file

I am trying to user the following code
private CModel[] getConfig(string CID, string Program)
{
ServiceManagement.ServiceClient obj;
List<ServiceManagement.ManagementApiRepositoryCConfig> executedService;
obj = new ServiceManagement.ServiceClient();
executedService = new List<SaServiceIdentityManagement.ManagementApiRepositoryCConfig>();
executedService = obj.getClubConfigSingle(CID, Program);
return executedService.Select(x => new CModel
{
CID = CID,
ProgramName = x.Name,
ProgramURL = x.Value,
}).ToArray();
}
and
using (StreamWriter w = File.AppendText("log.txt"))
{
Log("call 1", w);
Log("call 2", w);
}
What I am wanting to do is build a .txt, or xml, js/json file to log requests to the service
I am not sure why Im getting nothing added to the log.txt file
Thanks M
It sounds like you are trying to build something that already exists by default in the .net framework. I think what you're looking for is the tracing functionality that's in system.diagnostics. Try adding this to your config file:
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information,ActivityTracing"
propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\logs\TracingAndLogging-client.svclog" type="System.Diagnostics.XmlWriterTraceListener"
name="xml" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
It's easy and cheap to implement and there's even a special software that displays it neatly for you.
You can find more information about tracing here: https://msdn.microsoft.com/en-us/library/ms751526%28v=vs.110%29.aspx

Set logging level of autoflush .Net Trace Listener

In a .Net 4.0 web-service I am using trace autoflush to write to a log file.
By adding the following in the web.config:
<trace autoflush="true" >
<listeners>
<add name="TextWriter"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace.log" >
</add>
</listeners>
</trace>
Am trying to find a way to log only Trace.TraceError("error info") and exclude Trace.TraceInformation("some verbose debugging stuff") without altering my code and just changing the web.config?
The information I have found on MSDN shows how this can be done by adding code that calls Trace.Flush() and adding sources, switches and sharedlisteners, however I would like to continue using auto-flush and not alter the code.
Many thanks in advance :)
Old answer:
This appears to be impossible. Trace auto-flush does not have the
capacity to have it's level set in the web.config.
Update:
The listener may have a filter applied as follows
<configuration>
<system.diagnostics>
<trace autoflush="true" >
<listeners>
<add name="TextWriter"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace4.log" >
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning" />
</add>
</listeners>
</trace>
</system.diagnostics>
Note that the initializeData is a string and may take the values of Warning or Error. This filters the trace output accordingly.
Many thanks to Josip for following up on this question.
Maybe you can use "switchValue" for this purpose like this:
<system.diagnostics>
<trace autoflush="true">
</trace>
<sources>
<source name="SCInterface" switchType="System.Diagnostics.SourceSwitch" **switchValue**="All">
<listeners>
<remove name="default"/>
<add name="HSInterface"
type="XYZ.Diagnostics.CyclicTextTraceListener, XYZ.Base3x"
initializeData="D:\Trace\HSR.HSInterface.Trace.log, Size, 10, 5"
traceOutputOptions="DateTime,ThreadId,Callstack" />
</listeners>
</source>
</sources>
</system.diagnostics>
For switch value you would put Warning or Error...

Using Fiddler as proxy for HttpWebRequest requires Fiddler to be running

I have a web application that I'm developing that makes a lot of HttpWebRequests. In order to make debugging them easier, I've set up the following in my web.config;
<system.net>
<defaultProxy>
<proxy proxyaddress="http://127.0.0.1:9999" />
</defaultProxy>
</system.net>
This allows all of my HttpWebRequests to proxy through Fiddler. The problem is I need to have Fiddler running in order to have my app work correctly.
Ideally, I would like to have it proxy through Fiddler when Fiddler is running, and not proxy at all when Fiddler is not running without having to change my web.config each time.
There are a few options.
First, you can set the relevant Proxy property of the relevant objects inside your code directly instead of falling back to the configuration XML; you can then selectively control the use of the proxy based on any factor you like.
Alternatively, you could try setting the scriptLocation attribute to point at http://localhost:8888/proxy.pac and use Fiddler's about:config to set fiddler.proxy.pacfile.usefileprotocol to false and tick the Tools > Fiddler Options > Connections > Use PAC Script box.
Have you considered an another approach, by enabling trace listeners on System.Net? It is not as comfortable as using Fiddler, but it might be enough for sporadic debugging. For message logging, just System.Net should be sufficient, but there are more.
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<!--<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>-->
<!--<source name="System.Net.Cache">
<listeners>
<add name="System.Net"/>
</listeners>
</source>-->
</sources>
<sharedListeners>
<add
name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="System.Net.trace.log"
/>
</sharedListeners>
<switches>
<add name="System.Net" value="Verbose" />
<add name="System.Net.Sockets" value="Verbose" />
<add name="System.Net.Cache" value="Verbose" />
</switches>
</system.diagnostics>

Writing to a new log file each day with TraceSource

I am using a logger in my application to write to files. The source, switch and listeners have been defined in the app.config file as follows:
<system.diagnostics>
<sources>
<source name="LoggerApp" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="myListener.log" />
</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Information" />
</switches>
</system.diagnostics>
Inside, my .cs code, I use the logger as follows:
private static TraceSource logger = new TraceSource("LoggerApp");
logger.TraceEvent(TraceEventType.Information, 1, "{0} : Started the application", DateTime.Now);
What would I have to do to create a new log file each day instead of writing to the same log file every time?
What would I have to do to create a new log file each day instead of writing to the same log file every time?
You'd have to make your own TraceListener instead of using TextWriterTraceListener. This would allow your TraceListener implementation to change log files daily, or do any other custom behavior you wish.
Try use:
<system.diagnostics>
<sources>
<source name="LoggerApp" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="myListener-{0:dd}-{0:MM}-{0:yyyy}.log" />
</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Information" />
</switches>
</system.diagnostics>

How can I see what information my (c# implemented) POST is sending?

This is the code I'm using, (taken largely from another online source, btw):
string uri = "http://www.blah.com";
// Create my request
HttpWebRequest hwrOrdersIDCallback = (HttpWebRequest)WebRequest.Create(uri);
hwrOrdersIDCallback.KeepAlive = false;
hwrOrdersIDCallback.ProtocolVersion = HttpVersion.Version10;
hwrOrdersIDCallback.Method = "POST";
// Turn the req string into a byte stream
byte[] postBytes = Encoding.ASCII.GetBytes(sbOrderIDsLine.ToString());
// Set content type and stream length
hwrOrdersIDCallback.ContentType = "application/x-www-form-urlencoded";
hwrOrdersIDCallback.ContentLength = postBytes.Length;
Stream requestStream = hwrOrdersIDCallback.GetRequestStream();
// Send the POST
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
// Grab the response and display it in a label
HttpWebResponse hwrOrdersIDResponse = (HttpWebResponse)hwrOrdersIDCallback.GetResponse();
label1.Text = (new StreamReader(hwrOrdersIDResponse.GetResponseStream()).ReadToEnd());
I should be getting some specific data back from the server if the POST was completed successfully. I am NOT getting that data and I wanted to know if there was a way to see the information that is getting sent to the server by this POST.
Do you absolutely have to investigate programmatically?
The easiest way to see what's going on is either to use Fiddler or WireShark.
Fiddler2 is a great tool for debugging traffic for ajax/service calls. It monitors traffic, and you can view the details of the calls and see what data is posted and returned.
Use fiddler.
Create file named <your exe>.config in the application directory and place this inside it:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="System.Net.trace.log"/>
</sharedListeners>
<switches>
<add name="System.Net" value="Verbose" />
<add name="System.Net.Sockets" value="Verbose" />
</switches>
</system.diagnostics>
</configuration>
or use WireShark as others suggested. The trace technique is easier to setup, but harder to read.

Categories