How to check quitly if video stream is alive using http - c#

I know I can perform a normal HTTP get and check the response but in my case i have some limitations as the video feed server is only allowing 1 connection at a time, which mean any HTTP request while watching the video stream on any player will interrupt the main stream and make it stop.
I have tried different headers with the HTTP GET request (AddRange , ReadWriteTimeout) but all were causing the feed to stop playing on any player
I have tried the HEAD request but for some reason its stuck at GetResponse() as it try to download the content which goes into infinity
var request = WebRequest.Create("http://xyss.ts") as HttpWebRequest;
request.Method = "HEAD";
var response = request.GetResponse(); //doesn't stop trying to download content
request.Abort();
response.Close();
Any idea how we can test if the feed still exist without interrupting the main server feed so it doesn't affect any client streaming

Related

Web service Crash in background wp8

why the response of the web service crash if the app goes to Background after calling a web service? does anyone know a workaround to let web service work in background.
below in the second line of code my app crash
.....
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);//Crash
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
......
when App goes to Background it is put in the Sleep mode, thus, aborting all current activities. To prevent from this you can allow your app to run in background in WP8 settings.
Also, check this StackOverflow answer. It describes certain aspects regarding background support.

HttpWebRequest timeout not obeyed

I have set up the following web request:
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(gotoWebinerUrl);
request.Accept = "text/json";
request.Timeout = 5000;
// Allows us to track with Fiddler, for dev use only
request.Proxy = new WebProxy("127.0.0.1", 8888);
try
{
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
...
}
catch (Exception exception)
{
...
}
In Fiddler, I have installed this plugin which delays requests http://fiddlerdelayext.codeplex.com/. Using the plugin, I have added a rule which delays the webinar request URL by 60,000ms (1 min).
I would expect my application to delay for 5 seconds, fail and be caught by my exception. However, it delays for the full 60 seconds.
I'm not sure if this is the plugins problem or my application, but I suspect the latter. In the 60 seconds delay, I can navigate to other web pages and see the requests in Fiddler, so I don't believe the problem exists with the plugin.
I found a couple of similar questions (How to terminate HttpWebRequest Connection in C#?It doesn't work even set timeout or readwritetimeout), but I'm not very familiar with threads and if they apply here or not.
Additional Info
A bit more research into this stepping through the code and I've noticed some interesting behaviour. The GetResponse is fired successfully and I see the request made in Fiddler. I then have my 60 seconds wait time. However, when I can continue stepping through, I notice that it has been caught as a TimeoutException, despite the fact it's actually waited the full 60s and has received a successful response. This does suggest that maybe something in the plugin is fooling the application into holding on somehow.

HTTPWebRequest.GetResponse() failing with authenticated requests through a transparent proxy

We're using the HTTPWebRequest objects to make HTTP requests to our application and we're having a problem when the request requires authentication and there is a transparent proxy (Squid 3.1.10).
string url = "http://www.icode.co.uk/test/auth.php";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
MessageBox.Show(reader.ReadToEnd());
reader.Close();
stream.Close();
response.Close();
Our original code used the WebClient class which exhibited the same problem.
The first time this code runs, it displays the result correctly.
When the code runs a second time, it fails on the GetResponse() line with:
System.Net.WebException was unhandled
Message="The server committed a protocol violation. Section=ResponseStatusLine"
Source="System"
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at Dummy.DummyForm.button1_Click(Object sender, EventArgs e) in H:\Trial\Dummy\DummyForm.cs:line 42
at ...
On Windows 7, restarting the process causes it to recover and work once, but Server 2003 requires a full reboot.
Looking at the network capture, two requests are identical to start with, the initial unauthenticated request is sent and the server replies, but the failing requests sends the 2nd authenticated request in the middle of the initial reply as if it's ignoring the Content-Length header (which is correct). It then receives the rest of the initial reply and fails with the protocol error.
It does seem odd that the client (HTTPWebRequest) doesn't close the connection cleanly though.
When the proxy is not in use (non port 80 or internal traffic) the requests all work as expected. When there is no authentication, it also works as it only makes the single request.
I've already reduced the problem code to the minimum and reproduced it with the MSDN sample, but does anyone know if this is a known issue or a problem in our (.NET or Squid) configuration?
Since it only fails the second time, would
request.KeepAlive = false;
make a difference?
I think NTLM authentication (NetworkCredential) does not work at the same time with transparent proxy feature of SQUID. :-(
http://www.squid-cache.org/mail-archive/squid-users/201110/0025.html
Could you try another authentication scheme?
Try authenticating yourself, with
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
before the request.GetResponse();
This worked for me. First I tried putting in the whole string myself, which didn't work!

How to build TCP IP listner to read incoming messages using chilkat socket class c#

I am using chilkat socket class. The problem is I want to keep my socket open, lets say I executed my form and the very first time which opened the port on a specific IP to listen the messages.I am able to receive the messages first time only successfully, now after this message I want to keep my application to keep listening and receive when ever a new message comes.
We have several clients who will connect and send some text messages on the same port and ip.
But I am unable to achieve this. I need to build a Listener, which will keep on listening and as soon as I will get any message I need to process it. Any body who has used chilkat class or having experience in this kind of application kindly suggest me how can I achieve this functionality as I could't find good example for this kind of application on CHILKAT website or may be I am inexperienced don't know how to exactly code this type of functionality.
Edit 1: Jermy,
yes we have developed REST WCF services and they are working perfect, but the problem is in the response of REST WCF Service big response headers are appearing, which we don't want because in our enterprise application Windows Phone 7 mobiles will also communicate and send text messages and only for the sake of mobiles we are trying to reduce the data we need to pass back and by using sockets we can avoid extra response headers and SMS is not an option for us because of cost. If you have any suggestions towards Webservices to minimize the data kindly share it.
Have you considered a Web Service? They can be consumed by pretty much any language that can send Http requests. If you have control of the client applications then a Web Service is definitely the correct route.
http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html
Edit:
Have you considered simple http upload of bytes, with a http response code. Ie Http Ok, Http Failure. You can customize the status codes to anything that suits your project.
Edit 2:
Perhaps an RPC styled method with ONLY http status codes as the response could be suitable. Checks this question for hints. json call with C#
Basically you are just sending some string to a url, then receiving the status code back. That is quite minimal.
Edit 3:
Here is something I pulled out of some old code with Reflector. This is just for the general gist of the procedure. Obviously there should be a using statement on the first request.
public void SMS(Uri address, string data)
{
// Perhaps string data is JSON, or perhaps its something delimited who knows.
// Json seems to be the pretty lean.
try
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
request.Method = "POST";
// If we don't setup proxy information then IE has to resolve its current settings
// and adds 500+ms to the request time.
request.Proxy = new WebProxy();
request.Proxy.IsBypassed(address);
request.ContentType = "application/json;charset=utf-8";
// If your only sending two bits of data why not add custom headers?
// If you only send headers, no need for the StreamWriter.
// request.Headers.Add("SMS-Sender","234234223");
// request.Headers.Add("SMS-Body","Hey mom I'm keen for dinner tonight :D");
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.WriteLine(data);
writer.Close();
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
// Either read the stream or get the status code and description.
// Perhaps you won't even bother reading the response stream or the code
// and assume success if no HTTP error status causes an exception.
}
}
}
catch (WebException exception)
{
if (exception.Status == WebExceptionStatus.ProtocolError)
{
// Something,perhaps a HTTP error is used for a failed SMS?
}
}
}
Remember to respond only with Http status codes and descriptions. And ensure that the request's proxy is setup to bypass the requesting Url to save time resolving the IE proxy.

Monitor which thread is downloading url

I have an application that downloading urls using threadPool in different threads, but recently I've read an article (http://www.codeproject.com/KB/IP/Crawler.aspx) that it says HttpWebRequest.GetResponse() is working only in one thread and the other threads is waiting for that thread. first I want to know is it true ? how can i monitor which one of my threads is actually downloading with its status ?
I doubt that HttpWebRequest.GetResponse would block other threads - but you can verify that easily using tools such as Fiddler. You can launch fiddler and run your program. The request would appear in Fiddler as soon as your program makes it and you can quickly determine if they are simultaneous or one by one.
Yes, GetResponse is a blocking call (check MSDN) which can only return when the server replies or a request timeout occurs. After that, just check the status code and use GetResponseStream to start downloading the returning content. Like this:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == 200)
{
Stream content = response.GetResponseStream();
// Read the content and report the downloading progress...
...
}

Categories

Resources