How can I change the Input request stream - input

I want to change the input request stream.
Detail is:
I want to replace the 'Text 1' in the Input stream to 'Text 2'
The first: I use the Request.Filter as http://msdn.microsoft.com/en-us/library/system.web.httprequest.filter.aspx But this way I can only replace a byte data by an other byte, I can not replace a string by a string and I also can not change buffer size. I need to access and replace the old buffer by an other byte array (content and buffer.length)
The second: I try to use a ISAPI Filter, but It seem that the IIS 7 and above is no longer support for SF_NOTIFY_READ_RAW_DATA. I google and result is I should write a native HttpModule with CHttpModule::OnReadEntity event. Is it right ?
I would be very appreciate if you can tell me how I can change the input request stream.
Thank you very much.
Huy

I debugged and see that We will catch twice times per one request.
if I only read the entity body, then insert again, don't make any change, the second request will have exactly content and request run well. The source code as following:
// Retrieve the request body.
hr = pHttpRequest->ReadEntityBody(
pvRequestBody, byteCount, false, &byteCount, NULL);
pHttpRequest->InsertEntityBody(pvRequestBody, byteCount);
if I read the entity body, then replace 'ngo+duc+huy' by 'ngy+van+qua' (this is successful), the second request will have wrong content. This request have some strange content as: 'ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ'
so, the request haven't run property. The source code as following:
// Retrieve the request body.
hr = pHttpRequest->ReadEntityBody(
pvRequestBody, byteCount, false, &byteCount, NULL);
size_t found(0);
string requestContent = static_cast(pvRequestBody);
found = requestContent.find("ngo+duc+huy");
if(found !=string::npos)
{
requestContent = requestContent.replace(requestContent.find("ngo+duc+huy"), sizeof("ngo+duc+huy") - 1, "ngu+van+qua");
}
pvRequestBody = (void*)requestContent.c_str();<br />
pHttpRequest->InsertEntityBody(pvRequestBody, byteCount);<br />
you can download my simple solution at: http://www.4shared.com/file/OJr8GFJQ/HelloWorld_2.html

Related

How can i show the image name when i taking a picture?

Im trying to show the name of the image i take with my Camera but i dont know how.
Do i need a another function to show the image name?
My Code for taking picture:
Public Function takePicture() As String
Dim url = THETA_URL & "commands/execute"
Dim payload = New Dictionary(Of Object, Object) From {
{"name", "camera.takePicture"}
}
Dim request As Net.WebRequest = Net.WebRequest.Create(url)
request.Credentials = New Net.NetworkCredential(THETA_ID, THETA_PASSWORD)
Dim resp As Net.WebResponse = request.GetResponse()
End Function
See the line where is says:
End Function
Look in the line numbers margin; next to it there is a blank bar (mine is dark gray in my theme) - click in it to put a red dot:
End Function will go red too..
Then run your code and retrieve your image. The code will stop with a yellow bar pointing to End Function
Take a look at the bottom of VS - you'll either have an Autos window or a Locals window - either one will do. It will show the response object and you can drill into it like a tree, open the headers collection, have a look if anything in it contains the data you want.. It also thus tells you how to get the data you want out of it..
e.g. if I wanted the "Content-Disposition" value I could say resp.Headers("Content-Disposition") - AllKeys is showing me what available strings I can use to index the headers collection
Content-Disposition probably won't list a filename on its own - it'll be something more involved like "attachment; filename=someimage.jpg" so you'll need to pull the data you want out of it. Don't get your hopes up if this is a basic cam; it's unlikely to have any meaningful sort of filename. It might be IMG_0001 etc, if it's there at all - I think you should instead make your own name up, as you'll be able to put more info into it, it will be more meaningful than what you get from the cam (and if the cam doesn't send a filename you'll have to do it anyway)

How do you add a picture to the ID3 Album tag using taglib#

I've been searching the internet and trying various methods to save a picturbox image to the the id3 Album picture tag. One sample code says the Album cover tag name is taglib.ipicture another says taglibVariable.Image and yet another says taglibVariable.picture(0).
I am becoming so confused I'm starting to repeat sample test code.
Where is the documentation that will explain what I have to do.?
What little information I can find are dead links to sample code or incomplete code using variables without explanations. When I look up the commands and try to format or convert to the needed data type, I get an error. Usually system.image.bmp cannot be converted to iPicture.
Can anyone give me some working code or a pointer on how to word the proper search term to add a picturebox.image to the Album picture tag. Saving the image as a file then opening as image to put in tag then deleting file is not an option. I need to create a memory image and add that to the picture tag.
This is what I use:
public void SavePicture(string fileName, string picName) {
try {
IPicture[] pics = new TagLib.IPicture[1];
pics[0] = new TagLib.Picture(picName);
using (var songTag = TagLib.File.Create(fileName)) {
songTag.Tag.Pictures = pics;
songTag.Save();
}
}
catch {
// process
// mpeg header is corrupt
}
}
fileName is the full path to the audio file;
picName is the full path to the picture.
You can add multiple pics by setting the array size for the IPicture array accordingly...

Video streaming - dealing with header 206 (php to HTML5)

As usual, it never works for me...
I try to stream video and I'm doing very well with the code below:
function send_back_video($video_path)
{
ob_clean();
$mime = "application/octet-stream"; // The MIME type of the file, this should be replaced with your own.
$size = filesize($video_path); // The size of the file
// Send the content type header
header('Content-type: ' . $mime);
// Check if it's a HTTP range request
if(isset($_SERVER['HTTP_RANGE']))
{
// Parse the range header to get the byte offset
$ranges = array_map(
'intval', // Parse the parts into integer
explode(
'-', // The range separator
substr($_SERVER['HTTP_RANGE'], 6) // Skip the `bytes=` part of the header
)
);
// If the last range param is empty, it means the EOF (End of File)
if(!$ranges[1]){
$ranges[1] = $size - 1;
}
// Send the appropriate headers
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
header('Content-Length: ' . ($ranges[1] - $ranges[0])); // The size of the range
// Send the ranges we offered
header(
sprintf(
'Content-Range: bytes %d-%d/%d', // The header format
$ranges[0], // The start range
$ranges[1], // The end range
$size // Total size of the file
)
);
// It's time to output the file
$f = fopen($video_path, 'rb'); // Open the file in binary mode
$chunkSize = 8192; // The size of each chunk to output
// Seek to the requested start range
fseek($f, $ranges[0]);
// Start outputting the data
while(true)
{
// Check if we have outputted all the data requested
if(ftell($f) >= $ranges[1])
{
break;
}
// Output the data
echo fread($f, $chunkSize);
// Flush the buffer immediately
#ob_flush();
flush();
}
}
else {
// It's not a range request, output the file anyway
header('Content-Length: ' . $size);
// Read the file
#readfile($file);
// and flush the buffer
#ob_flush();
flush();
}
}
"So if it works, what do you want?" - you might ask..
As I stated before, the code does a very good job, BUT there is 1 issue.
If I start to play video, php sends header('HTTP/1.1 206 Partial Content') and this (video) request stays open till EOF, what means, I am FORCED to download entire video even if don't want to (let's say I'm clicking on the next video).
My JS removes video tag (this should ABORT request) and creates new video tag with new link. ABORT part is not happening.
Here you can see GET the example:
1st - initial video get
2nd - play button pressed
3rd - navigate away - new DOM item created and waiting to appear.
So what I'm looking for, is some option to close the connection on navigate away.
I saw youtube does many GETs. As I understand every GET brings new "chunk" when previous "chunk" close to end. It's very good option, but it cancels buffering and I want to be able to buffer video.
P.S.
For sure I'm doing something wrong, I just can't see what.
P.P.S.
I'm not the author of the code.

Attach photo to Activity through Google + Domains API

Im trying to post an activity with an image to a google+ domain with the new google + domains API
Posting an activity is working fine but when i try to attach a photo to it, i receive a 500 error with null description.
This is the code:
String msg = "Activity with photo";
// Create a list of ACL entries
PlusAclentryResource resource = new PlusAclentryResource();
resource.setType("domain"); // Share to domain
List<PlusAclentryResource> aclEntries = new ArrayList<PlusAclentryResource>();
aclEntries.add(resource);
Acl acl = new Acl();
acl.setItems(aclEntries);
acl.setDomainRestricted(true); // Required, this does the domain restriction
// Create a new activity object
Activity activity = new Activity()
.setObject(new Activity.PlusObject().setOriginalContent(msg))
.setAccess(acl);
// Attach the link
Activity.PlusObject.Attachments attachment = new Activity.PlusObject.Attachments();
attachment.setObjectType("photo");
attachment.setUrl( "http://c299813.r13.cf1.rackcdn.com/MuseeduLouvre_1335428699_org.jpg" );
attachment.setId( randomId ); //if not specified, google returns an error with "you must specify the photo id"
List<Activity.PlusObject.Attachments> attachments = new ArrayList();
attachments.add(attachment); // You can also add multiple attachments to the post
activity.getObject().setAttachments(attachments);
activity = plus.activities().insert("me", activity).execute();
When the code calls the execute, i receive this error:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 500
{
"code": 500,
"message": null
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
The same code but with the attachemnt lines commented works fine. Has someone managed to create an activity with an image? any clue?
Thanks in advance.
Directly attaching a photo via URL isn't possible. The process works slightly differently, as described here: https://developers.google.com/+/domains/posts/attaching-media
If you don't have the actual binary data of the photo you will first have to "download" the photo. Then you can upload the actual photo data via the media().insert method, which will give you the Photo Id which you can then use in attachment.setId().
setUrl isn't necessary in this case.
If you want to attach a photo as URL, this could also be handled like an article attachment (same as if you would just copy/paste the URL into a Google+ post). In that case you would use attachment.setObjectType("article") and only set the Url. The id isn't necessary in this case.

How to get an image from mshtml.htmlimg to hard disk

Without using API?
I know there are several way.
I am using mshtml library by the way, which is better than webbrowser control. I am effectively automating internet explorer straight.
Basically I prefer a way to take the image straight without having to know the URL of the htmlimg and download it.
I know I can take URL from the image element and downloading it with webclient. The image changes depending on cookies and IP. So that wouldn't do.
I want the exact images displayed by the htmlimg element to be the one stored.
Basically as if someone is taking a local screenshot of what shows up on screen.
There's an old solution for this here:
http://p2p.wrox.com/c/42780-mshtml-how-get-images.html#post169674
These days though you probably want to check out the Html Agility Pack:
http://htmlagilitypack.codeplex.com/
The documentation isn't exactly great however; so this code snippet may help:
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
// You can also load a web page by utilising WebClient and loading in the stream - use one of the htmlDoc.Load() overloads
var body = htmlDoc.DocumentNode.Descendants("body").FirstOrDefault();
foreach (var img in body.Descendants("img"))
{
var fileUrl = img.Attributes["src"].Value;
var localFile = #"c:\localpath\tofile.jpg";
// Download the image using WebClient:
using (WebClient client = new WebClient())
{
client.DownloadFile("fileUrl", localFile);
}
}