HTTPwebrequest image click post - post

So I am trying to navigate http://www.historicflyingclothing.com/shop.php by first clicking on an item in the dropdown. After posting the value from the dropdown using :
string poststring = String.Format("Cat1={0}", "7");
CookieContainer cookie = new CookieContainer();
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://www.historicflyingclothing.com/shop.php");
httpRequest.Method = WebRequestMethods.Http.Post;
httpRequest.CookieContainer = cookie;
httpRequest.AllowWriteStreamBuffering = true;
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.AllowAutoRedirect = true;
httpRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytedata = Encoding.UTF8.GetBytes(poststring);
httpRequest.ContentLength = bytedata.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
I was able to get the items on the page.
The problem is when I need to click on the next button. I was hoping to use the CookieContainer to help me navigate but I cant figure out what the post data should be. The html code for the next click is:
<form method="POST" class="shopform" action="shop.php">
<p style="text-align: center"> <input type="IMAGE" name="Submitnext" src="buttons/next.gif" value="npage" style="margin-bottom: -4pt"></p>
</form>
With the drop down the name was "Cat1" and value was "7" but what do I use for this IMAGE?

From the HTML you've provided and listed on that website, the values you need to post are:
Submitnext=npage
You'll noticed the name you're posting is contained in the "name" attribute and the value is contained in the "value" attribute.

Related

I would like to get the text of the button (of youtube) "subscribe"

I would like to get the text of the button (of youtube) "subscribe"
in a textbox in c #
example I open the form
and I receive this button text,
that is "subscribe"
in a textbox.
{
try
{
string url = this.textBoxLink.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
this.richTextBoxSC.Text = sr.ReadToEnd();
string imgpath = this.SearchY(this.richTextBoxSC, "thumbnailUl", 20, "\">").ToString();
this.pictureBoxThumb.ImageLocation = imgpath;
string title = this.SearchY(this.richTextBoxSC, "", 25, "").ToString();
if (title.Contains("\""))
{
title = this.SearchY(this.richTextBoxSC, "class=\"style-scope ytd-subscribe-button-renderer\"", 60, "
}

Convert input stream to image

I am try to convert inputstream to Image
I have tried using the below code.
var img = Image.FromStream(imagePathErrorMessage.StreamObject);
I need to pass the img to mvc view
#foreach (var img in imageSet)
{
<div class="thumbnail thumbnail-inline">
<label class="checkbox-inline">
<input type="checkbox" onchange="DisplayedImageOnchange(this)" value="#img.Id" />
<img src="#img.Url" class="img-thumbnail" />
</label>
<div class="btn-block">
<input type="file" class="replace file-loading" />
<button class="btn btn-xs btn-danger delete">delete</button>
</div>
</div>
}
public JsonResult Upload(object model)
{
var img = Image.FromStream(imagePathErrorMessage.StreamObject);
return Json(new AdCreative { Id = 100, Url = Url.Content("data:image;base64,#System.Convert.ToBase64String('" + img + "')") }, JsonRequestBehavior.AllowGet);
}
it throws the following error :
the above inputstream from amazon s3 response.
Firstly, you may find more information by clicking on 'View Detail' in the exception window, and expanding any InnerException properties.
Have you checked that imagePathErrorMessage.StreamObject is non-null and provides an image in one of the accepted formats?
These are JPG, BMP, TIF, GIF and PNG. If your image contains transparency data, you should convert it to a PNG first. You may not use RAW data, for that I think you'd want BitmapImage instead of Image
If the stream is valid, it will also need to be at position 0 before you start the read, for example:
imagePathErrorMessage.StreamObject.Position = 0;
var img = Image.FromStream(imagePathErrorMessage.StreamObject);
Edit for OP Edit:
Do you need to set the ResponseStream back to position zero after it is converted but before it is copied to destination?
(if at the breakpoint shown in your screenshot, you hovered over destination would it show a stream with 61153 length and CurrentPosition 0?)
Second Edit:
This answered post Loading an image from a stream without keeping the stream open describes why when using Image.FromStream then the stream needs to remain open for the lifetime of the image.

How to return status information during lengthy batch process in MVC 4 action?

In my MVC 4 app, I have a view that uploads a file from the client machine with:
<snip>
#using (Html.BeginForm("Batch", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input class="full-width" type="file" name="BatchFile" id="BatchFile"
<input type="submit" value="Do It" />
}
<snip>
The "Batch" action in the home controller takes that file and processes it in a way that may be very lengthy.... minutes even:
<snip>
[HttpPost]
public FileResult Batch(ModelType modelInstance)
{
// Do the batch work.
string result = LengthyBatchProcess(modelInstance.BatchFile.InputStream)
var encoding = new ASCIIEncoding();
Byte[] byteArray = encoding.GetBytes(result);
Response.AddHeader("Content-Disposition", "attachment;filename=download.csv");
return File(byteArray, "application/csv");
}
<snip>
This all works fine, and it isn't an inherent problem that the user is locked out for the time it takes for the batch process to run. In fact they expect it. The problem is that the user may not be in a position to know whether this process will take a couple of seconds or a couple of minutes, and I would like to provide them with status information while LengthyBatchProcess is running. I have researched unobtrusive ajax, but it does not seem to have the functionality necessary for this, unless there is some way to chain unobtrusive ajax calls. Any thoughts on how to best architect this? Many thanks in advance.
What you want to achieve requires a bit of work.
One way is to open another channel (ajax call) to get the progress report. Quoting from How do you measure the progress of a web service call?:
Write a separate method on the server that you can query by passing the ID of the job that has been scheduled and which returns an approximate value between 0-100 (or 0.0 and 1.0, or whatever) of how far along it is.
I've found a great tutorial on this matter.
Yes, you can start downloading the file in chuncks so the user can see the download progress of the browser:
try
{
// Do the batch work.
string result = LengthyBatchProcess(modelInstance.BatchFile.InputStream)
var encoding = new ASCIIEncoding();
Byte[] byteArray = encoding.GetBytes(result);
Response.Clear();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("Content-Disposition",
"attachment;filename=download.csv");
Response.ContentType = "application/csv";
Response.BufferOutput = false;
for (int i = 0; i < byteArray.Length; i++)
{
if (i % 10000 == 0)
{
Response.Flush();
}
Response.Output.WriteLine(byteArray[i]);
}
}
catch (Exception ex)
{
}
finally
{
Response.Flush();
Response.End();
}

Date format changes to 'mm/dd/yyyy' when posting data to web api method

I am using ASP.NET MVC 4 with Web API.
I want to be able to download a csv file on click on a button.
Below is my jquery call to the api 'exportfruit'
function downloadFile(){
var data = {
StartDate: this.model.get('StartDate'),
Name: this.model.get('Name')
};
var form = document.createElement('form');
form.action = 'api/fruitapi/exportFruit';
form.method = 'POST';
form.style.display = 'none';
for (i in data) {
if (data[i] != "") {
var inputElement = document.createElement('textarea');
inputElement.name = i;
inputElement.value = data[i];
form.appendChild(inputElement);
}
}
document.body.appendChild(form);
form.submit();
}
and my web api action is as below
[ActionName("ExportFruit")]
public HttpResponseMessage PostExportFruit(SomeModel model)
{
// for now i am just testing the value returned from model.
string csv = "some data from db";
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(csv);
//a text file is actually an octet-stream (pdf, etc)
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
//we used attachment to force download
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "testfile.csv";
return result;
}
now the problem I am facing is that I am passing date as 'dd/mm/yyyy' but in the web api action it converts date into 'mm/dd/yyyy'
So for example,
if I have a date like
1/2/2012 this is converted to 2/1/2012
if 22/10/2012 (todays date) is converted to 01/01/0001
How do I fix this ?
I had similar problem when passing json data which I fixed by using this But I have no idea on how to go about on this one.
Please help me on this, as there is hardly any content available on internet for this.
You can change StartDate's type, Datetime to String.
Then, post to action, ParseExact string to datetime
DateTime startDate = DateTime.ParseExact(model.StartDate, "dd/MM/yyyy", null);
DateTime endDate = DateTime.ParseExact(model.EndDate, "dd/MM/yyyy", null);

how to screen scrape and put valid href link that will go to that link

I have scraped a webpage but I want the link to have valid link and will jump to that linkpage when clicked.
ex scraped data: day 1 - Go to my Page - status
I want the Go to my page to jump to whatever link is in its href.
ex. actual html I got
<td>Go to my Page</td>
I need it to be like this:
<td>Go to my Page</td>
here's my code for scraping:
public string ScreenScrape()
{
string url = "http://somewebsite.com/tab/form/index.php";
string strResult = "";
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
var webGet = new HtmlAgilityPack.HtmlWeb();
var doc = webGet.Load(url);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href]"))
{
HtmlAttribute att = link.Attributes["href"];
att.Value = "http://somewebsite.com/tab/form/"+att.Value;
}
return strResult;
}
Here's my attempt to change the link and remove the javascript string but could not figure out how to get to the right index . Also, once I am able to change that, how do I replace each href in the strResult(above) to the new href?
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href]"))
{
HtmlAttribute att = link.Attributes["href"];
att.Value = "http://somewebsite.com/tab/form/" + ....
}
Can anyone pls help me? thanks
nevermind I got it, but I know the html url parsing is not the best way (if you have suggestion on how to parse it better, pls do so). right now, the only goal is to change the href link so here it goes.
public string ScreenScrape()
{
string url = "http://somewebsite.com/tab/form/index.php";
string strResult = "";
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
var webGet = new HtmlAgilityPack.HtmlWeb();
var doc = webGet.Load(url);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href]"))
{
string removeString ="javascript:jsFormAuth('";
string removeEnd = "');";
HtmlAttribute att = link.Attributes["href"];
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(att.XPath, "(");
string sub1 = att.Value.Replace(removeString,"");
string sub2 = sub1.Replace(removeEnd,"");
att.Value = "http://somewebsite.com/tab/form/" + sub2;
}
return doc.DocumentNode.InnerHtml;
}

Resources