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.
Related
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
I would like to know how can I fix this issue wherein a WebApp running on IIS 7/8 with Windows Authentication is throwing 401 error while executing HttpWebRequest to another site. This WebApp works fine if I run it locally i.e debug mode.
Here is the code snippet
HttpWebRequest webReq;
webReq = (HttpWebRequest)WebRequest.Create("http://sharepoint_site/_vti_bin/listdata.svc/mylist);
webReq.Accept = "application/json";
webReq.UseDefaultCredentials = true;
webReq.Credentials = CredentialCache.DefaultNetworkCredentials;
//webReq.Credentials = new NetworkCredential("user","password","domain");
webReq.Method = "GET";
webReq.KeepAlive = true;
Stream objStream = webReq.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
I was also able to make it work by adding BackConnectionHostNames entry in the registry
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
but I need to pass in the credentials (commented above) which I don't like because I don't want to use my account or any service account.
I want the WebApp to use DefaultNetworkCredentials or DefaultCredentials. I enabled Windows Authentication and NTLM provider on the IIS of the machine hosting this WebApp.
Any help will be greatly appreciated, thanks and more power to this community.
CredentialCache.DefaultNetworkCredentials uses the network credentials that the process is running under. If it's running in IIS, it will be the application pool identity, which the web service won't accept.
You will either have to pass different credentials in code (what you said you didn't want to do) or update the application pool to run with network credentials (right-click the application pool in IIS -> Advanced Settings -> Identity)
I have a WinForms app that calls a WCF service hosted on IIS7. For some reason, computers attached to the client network get a 401 Unauthorized error when trying to connect to the WCF Service through the WinForms app. I have spent quite a bit of time trying to figure this out and this is what I have found out so far.
The WinForms app receives the WCF service data on any other PC not on the client network.
From the client network, I can browse to the WCF service through a web browser just fine.
From the client network, I can browse to a service method and get the "Method not allowed"
error as expected.
Also, it should be noted that the WCF service is using REST instead of SOAP.
Here is the code I use to do all the service calls:
byte[] dataStream = Encoding.UTF8.GetBytes(strParameters);
WebRequest webRequest = WebRequest.Create(strUrl + strFunction);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.ContentLength = dataStream.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string responseString = responseReader.ReadToEnd();
Does anyone have any clues as to why I would be getting the 401 error only on the Client's network?
My suggestion would be to try the call via fiddler from the clients machine and validate if you can get the expected result via a POST. If this is successful I would look into how you are hosting the service in IIS. I have seen on several occasions where the issue was Anonymous or Basic Authentication was turned off which caused similar issues.
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!
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...
...
}