hi
i am using httpwebrequest and httpwebresponse to send request and get response respectively.
i want to keep connection live after sending one request in order to send another request using same channel. so plz help me.
HTTPWEBREQUEST.KEEPALIVE = TRUE is not working.

how to keep connection live while using httpwebrequest?
Babroberg
Kavita
The WebRequest class needs to be created for each request. I am not seeing the
webrequest being created for the second one.
1. Keep-Alive is true by default.
2. That does not guarantee that we will reuse the connection.
3. If you are authenticating [which in your case does not seem to the case], there is no guarantee that the connection is reused.
4. If the server closes the connection the connection cant be reused.
So in general Keep-Alive = true should result in reuse of connections but
Not guaranteed.
That said, you can use the System.Net.tracing
http://blogs.msdn.com/dgorti
to enable tracing and see how many connections are being created
This works only for System.Net 2.0
For System.Net 1.1 and 1.0 A network trace should give you a very clear picture of what is happening with your connections
caaptain
I've a problem in my soap based application. There are some dll files which were downloaded and added in my dll list. Now the problem is there's an error popping out which is related to the security. i even gave FullTrust to the downloaded dll files and still the problem persists.Since the dll file is not accessed theres a problem in database connectivity.The Connection status is alwalys closed and im not able to Open it.
The Full Trust for the files were given using .Net Configuration 1.1 and even caspol was used.
if anyone has got the solution then do send me the reply
exception thrown:
Additional information: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.TypeInitializationException: The type initializer for "Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionFailedEvent" threw an exception. ---> System.Security.SecurityException: Requested registry access is not allowed.
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at System.Diagnostics.EventLog.CreateEventSource(String source, String logName, String machineName, Boolean useMutex)
at System.Diagnostics.EventLog.CreateEventSource(String source, String logName, String machineName)
at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.PerformanceCounterInstances.ReportCounterFailure(String message)
at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.PerformanceCounterInstances..ctor(String categoryName, String counterName, Boolean createNewInstance)
at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.AddPerformanceCounter(String category, String[] counterNames, Boolean createNewInstance)
at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.Initialize(String counterCategory, String[] counterNames, Boolean createNewInstance, String eventLogSource, EventLogIdentifier[] eventIds)
at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent..ctor(String counterCategory, String[] counterNames, Boolean createNewInstance, String eventLogSource, EventLogIdentifier[] eventIds)
at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataServiceEvent..ctor(String[] counterNames, EventLogIdentifier[] eventLogIds)
at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionFailedEvent..ctor(String[] counterNames, EventLogIdentifier[] eventLogIDs)
at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionFailedEvent..cctor()
--- End of inner exception stack trace ---
at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionFailedEvent.Fire(String connectionString)
at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataInstrumentationFacade.ConnectionFailed(String connectionString)
at Microsoft.Practices.EnterpriseLibrary.Data.Database.OpenConnection()
at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteReader(DBCommandWrapper command)
at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteReader(String storedProcedureName, Object[] parameterValues)
at GeneriCo.ProductSystem.DataLayer.NewProductData.GetProduct(String sku) in c:\program files\generico\product system\source code\datalayer\newproductdata.cs:line 31
at GeneriCo.ProductSystem.BusinessLayer.ProductLogic.GetProduct(String uniqueProductId) in c:\program files\generico\product system\source code\businesslayer\productlogic.cs:line 33
at GeneriCo.ProductSystem.ServiceLayer.GetProductService.GetProduct(GetProductRequest request) in c:\inetpub\generico\product system\getproductservice.asmx.cs:line 26
--- End of inner exception stack trace ---
CHolmes
//1st request
webrequest = (HttpWebRequest)HttpWebRequest.Create(sLink);
webrequest.KeepAlive=true;
//webrequest.Pipelined = true;
webrequest.Timeout =3000;
webrequest.Method = "POST";
webrequest.UserAgent= "beone/v1.0";
webrequest.Headers.Add("Beone-Encoding: " + gwid + "\n");
webrequest.ProtocolVersion = HttpVersion.Version10;
webrequest.Connection = "keepalive";
string sdata = "Testing";
byte [] data = enc.GetBytes(sdata);
Stream stream = webrequest.GetRequestStream();
stream.Write(data,0,data.Length);
stream.Close();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
StreamReader reader = new StreamReader(webresponse.GetResponseStream());
char[] readbuffer = new char[256];
int count = reader.Read(readbuffer,0,256);
while(count>0)
{
string outputdata = new string(readbuffer,0,count);
buffer = buffer + outputdata;
count = reader.Read(readbuffer,0,256);
}
MessageBox.Show(buffer);
//2nd request
string sdata = "Testing";
byte [] data = enc.GetBytes(sdata);
Stream stream = webrequest.GetRequestStream();
stream.Write(data,0,data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)webrequest.GetResponse();
StreamReader reader1 = new StreamReader(webresponse.GetResponseStream());
char[] readbuffer1 = new char[256];
int count1 = reader.Read(readbuffer1,0,256);
while(count1>0)
{
string outputdata1 = new string(readbuffer1,0,count);
buffer = buffer + outputdata1;
count = reader.Read(readbuffer1,0,256);
}
reader.Close();
webresponse.Close();
response.Close();
Michal Malecki - MSFT
Generally you will have better luck posting an issue as a new thread than to tag on to the one that is already answered.
That said, I am not clear as to what exactly the question is.
ServicePoint is just a holder of connection groups which hold connections.
The KeepAlive are the semantics of a HTTP Request that goes on these connections.
Webrequest does not close the streams for you at this point. While there are
reasons why this is difficult to automagically for you, we are looking into heuristics of when this can be done automatically. for example, you read the stream completely and then we can possibly release the connection. We are looking into this in the future release
Arobind
can u post some code that shows the problem.
perjorgen
I m kavita i ve a problem in httprequest.when i try to send 2 requests in one function using keepalive = "true" it is using single channel to send request to server. but when i tried same thing using two threads and calling two diff. functions then it is creating another channel for second request.
so what should i do in order to use single channel to send more than one request.
i am using https request.
//thread code
public Cserver()
{
objhttps = new https();
try
{
thread1 = new Thread(new ThreadStart(fnfirstthread));
thread1.Start();
thread2 = new Thread(new ThreadStart(fnsecondthread));
thread2.Start();
}
catch (Exception e)
{
//gwslog .Info("An Exception has occurred! " + e.ToString());
thread1.Abort();
thread2.Abort();
}
}
public void fnfirstthread()
{
objhttps.sendmsg();
}
public void fnsecondthread()
{
objhttps.resendmsg();
}
}
//two request in one fun.
HttpWebRequest webrequest1 = (HttpWebRequest)HttpWebRequest.Create(sLink);
webrequest1.KeepAlive=true;
webrequest1.Method = "POST";
webrequest1.UserAgent= "beone/v1.0";
webrequest1.Headers.Add("Beone-Encoding: " + gwid + "\n");
Stream stream1 = webrequest1.GetRequestStream();
stream1.Write(data,0,data.Length);
stream1.Close();
HttpWebResponse response = (HttpWebResponse)webrequest1.GetResponse();
StreamReader reader1 = new StreamReader(response.GetResponseStream());
char[] readbuffer1 = new char[256];
int count1 = reader.Read(readbuffer1,0,256);
while(count1>0)
{
string output = new string(readbuffer1,0,count);
buffer = buffer + output;
count1 = reader1.Read(readbuffer1,0,256);
}
MessageBox.Show(buffer);
regards
kavita
djlins
This is what is going on.
Internally we look at the max connection limit. We will open connections up to that limit as needed. Now the default connection limit is 2.
When you are using two threads, two requests are going on in parallel, when one of the connections is being used by one request, we realize that a second request need to be dispatched and then since we are under the connection limit, we open a new connection.
When you are using one thread, we realize that the one connection wehave opened is free after the first request so there is no reason to open a second connection. This explains why we have reused the first connection.
And finally you can force all requests to go through a single connection my setting the servicepoint.ConnectionLimit
Like
WebRequest.ServicePoint.ConnectionLimit = 1
But then you may want to know that all requests for that host are serailzed. When one request takes a long time, the other requests are put in the queue in a
pending state and they may time out.
Hope that helps
Jaewon
I am writing a proxy that handles quite some load (towards a single remote system), and it seems to me like the appropriate way to do this is to cache a fair amount of WebRequest classes and never close their streams
It all seems a bit illogical to me, can someone explain how this works I thought the Service Point was handling the pooling Is the service point handling the keepalive as well Or are there two distinct layers here, where the pooling in the Service Point is distinct (and separate) from the streams and keep-alives within the HttpWebRequest
Slightly confused,
Kristian Rosenvold
rlo
i just want ot know after sending request if i send another request at that time will it create another connection or it will use same connection channel to send request to server.
if it creates new connection for each request then it will become slow.
so i want to send request using same channel means connection should not break after sending one request.
plz help me.
Luis Ricardo Velho
If this property is set to true, the connection used to retrieve the response remains open after the authentication has been performed. In this case, other requests that have this property set to true may use the connection without re-authenticating. In other words, if a connection has been authenticated for user A, user B may reuse A's connection; user B's request is fulfilled based on the credentials of user A.
Ofer Gal
Kavita,
First this is a different issue that the HTTP Connections. I suggest you start a different thread.
Now to your question:
Code access security does not give you more permissions than the user is gievn on the machine. In your case the issue is clearly that the registry key access is not allowed.
Please use Regmon www.sysinternals.com and see what key is getting access denied or something like that. You might be able to fix the registry key access.
Anwar Javed
jcbrooks75
Hi
I got stuck up with the same thing as what kavitha told .. but the scenario is somewhat different. As the connection is not persistent i cant set the keepalive property. Plus autoredirection is not allowed as its an VPN. So how can i move about Any solution other than settng the keepalive property of request . the other thing is the page contains two request/response .. error occurs in first request ..... im closing all the connections that i opened in final of try/catch block.... "Underlying connection closed! unable to connect to remote server" is the error coming after 3 or 4 hits to the page using a mobile ..... ( mobile .net page )