Save Webcam Image From Website - c#

There is a website that posts a image from their webcam that I would like to be able to grab using a program. How would I get this jpeg data in memory? I have tried HttpRequest but it only returns html.
Here is the link:
http://bigwatersedge.axiscam.net/view/snapshot.shtml?picturepath=/jpg/image.jpg

Image source is at this URL:
http://bigwatersedge.axiscam.net/jpg/image.jpg?timestamp=
Use WebClient to save the data.
WebCleint wc = new WebClient();
byte[] data = wc.DownloadData("http://bigwatersedge.axiscam.net/jpg/image.jpg?timestamp=");

The URL of the image is actually http://bigwatersedge.axiscam.net/jpg/image.jpg?timestamp=. The URL you gave returns a page with an image on it.
Now, once you are downloading the correct URL of the image, this particular server is checking your HTTP referer. Put this in your HTTP headers:
Referer: http://bigwatersedge.axiscam.net/view/snapshot.shtml?picturepath=/jpg/image.jpg

I am working with Robert, the original poster on this, and have a problem getting to that URL with the image. If the URL is pasted in the browser:
403 Forbidden
Your client does not have permission to get Images/Streams from this server.
Which is new, I was getting the URL text repeated in the browser:
http://bigwatersedge.axiscam.net/jpg/image.jpg?timestamp=
WebClient throws and exception:
"The underlying connection was closed: The connection was closed unexpectedly."
I also added:
WebHeaderCollection headerCollection = new WebHeaderCollection();
headerCollection.Add("http://bigwatersedge.axiscam.net/view/snapshot.shtml?picturepath=/jpg/image.jpg");
wc.Headers = headerCollection;
Basically, I am having trouble retrieving the image in the URL.
update:
I added the HttpRequestHeader.Referer to the header collection.
No exception throw, collects the image data. Have not converted the byte array to a workable image object yet.

Related

How do I create a System.IO.Stream from blob:https://localhost:5001/2b28e86c-fef1-482e-ae16-12466e6f729f

I'm submitting a trusted url path to my webapi and then trying to upload the image to azure-storage. The uri I have to upload includes blob:https://localhost/... Which points to a image stored locally. I need to read this stream however I'm receiving an exception on first line of code:
"The URI prefix is not recognized."
var req = System.Net.WebRequest.Create("blob:https://localhost:5001/2b28e86c-fef1-482e-ae16-12466e6f729f");
using (var stream = req.GetResponse().GetResponseStream())
{
containerClient.UploadBlob(image.guid.ToString(), stream);
}
I did a bunch more searching and discovered that only the browser can access blob: files. Ended up switching back to FormData.

How to "create the video" after file uploaded to DailyMotion using c#

Im following the instructions from here to publish a new video on DailyMotion, using c# and a WebClient.
i successfully got the auth-token, then an upload url, then the actual file to upload. im stuck at step 4, called: "create the video"
it states to POST url=<the url i got from previous step> to https://api.dailymotion.com/me/videos (with the Authorization token in the header), but all my attempts result in "bad request" - without further explanation.
any ideas?
using (var client = new WebClient())
{
var createRequest = $"url={videoUpload.url}";
client.Headers.Add("Authorization", $"Bearer {authToken.access_token}");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var createVideo = client.UploadString("https://api.dailymotion.com/me/videos", "POST", createRequest);
}
also tried:
var createRequest = $"url={HttpUtility.UrlEncode(videoUpload.url)}";
I tried your code and my video was created successfully. As explained in our documentation a 400 error is related to a missing/invalid parameter.
I assume you are trying to send the upload url (returned in step 2) instead of the url returned by step 3 (url of your uploaded file).
You can find an article (with examples of returned values) which use a simplified way to upload on Dailymotion here.

Get Solvemedia image using RestSharp

I'm developing an application which needs to make a query on a website to extract certain data, such as the user's name, points / remaining balance etc.
I present a problem in the login, the client needs to solve a captcha solvemedia to be able to enter the website, I would like to extract the image of this captcha and show it to the client but I am having problems to extract it, i'm trying to do it through http requests with RestSharp, the reason I do not do it with a webbrowser or selenium is that it spends a lot more resources.
i try this:
RestClient restClient = new RestClient(#"//api-secure.solvemedia.com/papi/media?c=2#gAB09NHSertXLv3TnpobmKDxvkjsaT4m#X4wLMdkN.u0ENU8bgrS3KH9APTC4lJjokJaIfZePPIgNLL84QkOaQlXcxzHvOVTTU98Of7mo8BoC0QQuiH1RMqMrGof6BbL-tReeY8AHhPA7-nwvQKLqUEXQwTL4HhLXfZVre9jccpqQxFGIRYZH1ZQoAKCV5k1TGCLXXP9vMVsJFntDNz6Ozik02MANT1siBJRYTNIpGcj6p6Gbq5j0HvQChz7jtgdzwlj7nee0BdZphpg27ikQlVB5IUelMvSjzNNvPZawB9YbC9v6zyJngNQaJIJku2SPJkhFXIK0uoA;w=300;h=150;fg=000000;bg=f8f8f8");
var fileBytes = restClient.DownloadData(new RestRequest("#", Method.GET));
File.WriteAllBytes(Path.Combine(directory, "poster-got.jpg"), fileBytes);
The problem with this is that I only get an image that says "Media Error", Is there any way to get the image that is sent when you request the login page? Can it be done with restsharp? , if not with what library could I do it?
For downloading image I use https://github.com/jgiacomini/Tiny.RestClient
But when I try to view your image in in my browser I have a media error. I think you use a wrong url.
In your case
using Tiny.RestClient;
var client = new TinyRestClient(new HttpClient(), "#"http//api-secure.solvemedia.com");
FileInfo fileInfo = await client.
GetRequest("papi/media").
AddQueryParameter("2#gAB09NHSertXLv3TnpobmKDxvkjsaT4m#X4wLMdkN.u0ENU8bgrS3KH9APTC4lJjokJaIfZePPIgNLL84QkOaQlXcxzHvOVTTU98Of7mo8BoC0QQuiH1RMqMrGof6BbL-tReeY8AHhPA7-nwvQKLqUEXQwTL4HhLXfZVre9jccpqQxFGIRYZH1ZQoAKCV5k1TGCLXXP9vMVsJFntDNz6Ozik02MANT1siBJRYTNIpGcj6p6Gbq5j0HvQChz7jtgdzwlj7nee0BdZphpg27ikQlVB5IUelMvSjzNNvPZawB9YbC9v6zyJngNQaJIJku2SPJkhFXIK0uoA;w=300;h=150;fg=000000;bg=f8f8f8").
DownloadFileAsync("c:\"poster-got.jpg");

How to access secure url using webclient

I am trying to access secure url via web client and getting few errors.
Overall purpose is download an image from secure url..
Please note that earlier, I was able to download image from repository with http and now they have enforced (security) https in same url.
We have a console application which runs on periodic basis and pulls image from server and stores locally.
Please advise as what I am doing wrong ? or what is missing ?
Following are the issues I am facing.
/// if I use code section 1... I get error saying Parameter is not valid.
/// if I use code section 2... File gets saved to local but get a message " File appear to be corrupt or large and not able to open.
Section 1 and 2 are listed below.
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
webClient.Credentials = new NetworkCredential("id", "pwd"); // if credentials are wrong...still I get the imageByte
/// section 1 - if I use this code... I get error saying Parameter is not valid.
// ----------------------------- Start --------------------------------
imageByte = webClient.DownloadData("https://someurl/source/abcd.jpeg");
////Initialize image variable
Image newImage;
////Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(imageByte, 0, imageByte.Length, true))
{
ms.Write(imageByte, 0, imageByte.Length);
//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true, false); // Throws error at this line saying that parameter is not valid.
}
newImage.Save(#"location\image.jpeg");
// ----------------------------- End --------------------------------
/// section 2 - if I use this code... File gets saved to local but get a message " File appear to be corrupt or large and not able to open.
// ----------------------------- Start --------------------------------
webClient.DownloadFile("https://someurl/source/abcd.jpeg", #"location\image.jpeg");
// ----------------------------- End --------------------------------
}
Both errors say that the data which was downloaded is not a valid JPEG image. Likely it is because some error was returned from the server.
In order to check if server returned an error, you can take a look at what's in HTTP response headers and body.
For body you can just try to open a file saved in section 2 with a text editor.
For headers you can check webClient.ResponseHeaders property with debugger or with a simple update to your program.
Example below is taken from MSDN:
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
WebHeaderCollection myWebHeaderCollection = myWebClient.ResponseHeaders;
Console.WriteLine("\nDisplaying the response headers\n");
// Loop through the ResponseHeaders and display the header name/value pairs.
for (int i=0; i < myWebHeaderCollection.Count; i++)
{
Console.WriteLine ("\t" + myWebHeaderCollection.GetKey(i) + " = " + myWebHeaderCollection.Get(i));
}
Another ways to see what was actually returned from the server are Invoke-WebRequest PowerShell cmdlet or wget utility (both should support HTTPS and authentication)
You're code works fine.
As Kel suggested most likely the server response header give may give an answer. Are you sure the server is configured to support https?
There is no need to give network credentials. Since HTTPS only is a transport protocol. It secures only the connection.
To do authentication you have to configure the server for example to do BASIC or DIGEST authentication. And if required setup authorization to the requested resource.
One comment on you're code: Don't forget to dispose newImage.

Exception in BackgroundUploader

I am trying to upload some avi file to server. It works fine with HttpRequest but i need to continue uploading even if i suspend app so thats why i am trying to use BackgroundUploader. I am following this guideline on msdn http://msdn.microsoft.com/en-us/library/windows/apps/jj152727.aspx. So my code looks something like this.
StorageFile storageFile = KnownFolders.VideosLibrary.GetFileAsync("fileName");
BackgroundUploader uploader = new BackgroundUploader();
uploader.Method = "POST";
uploader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
var fs = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
IInputStream aaaa = fs.GetInputStreamAt(0);
UploadOperation upload = uploader.CreateUploadFromStreamAsync(new Uri("uploadUri"), aaaa);
await HandleUploadAsync(upload, true);
the rest is same as on MSDN. And i am getting exception Unsupported media type (415) in method HandleUploadAsync on line
await upload.StartAsync().AsTask(cts.Token, progressCallback);
What am i doing wrong? Or what can cause this kind of exception?
EDIT : I solved my problem as i commented down here and in my answer. I think at the end i am basically just sending some data to server that are recognized and interpreted as i want to. So if i use BackgroundUploader i am not only uploading some file i am also sending information about how am i doing that(as i mentioned in my answer). So by the same idea i can also upload folder to server and by that i am not sending any actual content only some description about how to do that. And if i compare request that i am making by HttpRequest and BackgroundUploader they are equal and thats what i wanted.
So the problem part was the header of request. I have some header in my request that is recognized by server and i was trying to put it to BackgroundUploader through SetRequestheader method but it did not work. As Kieqic suggested i used Fiddler and by that i compare request made by HttpRequest and BackgroundUploader. I found out they are completely different. So through SetRequestheader i add some parts like expected Content-Type and for the rest parts of header to make them equal i add it before content of my file as array of bytes. And this works so conclusion is in my case using Fiddler that helped my how to construct request header.

Categories