WebClient in C++ - c++

I'm writing a program that needs to be able to read in HTML source code into a string.
I've read about WebClient for C# but I need to write my program in C++ and I'm not sure how to do that (I've never used WebClient before).
Can anyone give me a simple C++ example program showing me how to get HTML source code into a string using WebClient? (or any better method)
Thanks.

See this page, A Fully Featured Windows HTTP Wrapper in C++:
http://www.codeproject.com/Articles/66625/A-Fully-Featured-Windows-HTTP-Wrapper-in-C
Sample code from that page, looks like what you want:
void ProgressTest(void)
{
// Set URL and call back function.
WinHttpClient client(L"http://www.codeproject.com/", ProgressProc);
client.SendHttpRequest();
wstring httpResponseHeader = client.GetResponseHeader();
wstring httpResponseContent = client.GetResponseContent();
}

I don't know what webclient for c# is. To read a file into a string-:
std::ifstream ifs("webpage.html");
std::string str;
str.assign((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));

Related

How to reset header buffer in Lucee?

I am trying to write a unit test for my ColdBox application running on Lucee 4.5 using testbox for a function that includes a cfhtmlhead() call.
Unfortunately, the string that would normally be appended to the <head> section of the HTML output using that function is appended to the output of the unit test instead, causing the test to fail.
The output of cfhtmlhead() is obviously written to a special buffer. According to a blog post it is possible to clear that buffer. The example function shown there looks like this:
function clearHeaderBuffer() {
local.out = getPageContext().getOut();
while (getMetaData(local.out).getName() is "coldfusion.runtime.NeoBodyContent") {
local.out = local.out.getEnclosingWriter();
}
local.method = local.out.getClass().getDeclaredMethod("initHeaderBuffer", arrayNew(1));
local.method.setAccessible(true);
local.method.invoke(local.out, arrayNew(1));
}
Though the blog post is written for Adobe ColdFusion and it obviously doesn't work the same way in Lucee.
By dumping local.out I saw that the object has a method resetHTMLHead(). But calling that method doesn't seem to work, either (even when the related getHTMLHead() method outputs the string from the cfhtmlhead() call).
So, how to reset the header buffer in Lucee?
I found the answer by checking the Lucee sources. There the buffer is accessed via getRootOut().getHTMLHead().
So the code to clear the header buffer boils down to this:
function clearHeaderBuffer() {
getPageContext().getRootOut().resetHTMLHead();
}

Working with Android InputStreams in native c code

Can anyone give me a hint how to work with Android InputStream in native code.
More specific example:
Java code
public class SomeParser {
public native ArrayList<String> parse(InputStream stream);
}
I need to read InputStream in native and return matching patterns to Android Java code.
stream is BufferedInputStream from HttpRequest
You needd to pass a reference to the Stream through JNI to your native code, and then use JNI calls to act upon it. You will probably get java byte-arrays. These you can copy to native arrays using JNI. It is all standard JNI.

WinRT and missing Web API models for Amazon API access

I was working with porting the sample from the link below to a Windows 8 Metro styled app
http://aws.amazon.com/code/Product-Advertising-API/2480
Looks like many features from the web model are removed (or moved) in WinRT:
HttpUtility.UrlEncode
HttpUtility.UrlDecode
HMAC / HMACSHA256
to name a few. Are there alternatives to these on WInRT? I looked online and there's very little insight.
Theres source code for URLDecode here, and looks like Uri.EscapeDataString can be used for Encode.
http://www.koders.com/csharp/fid1A50096D8FA38302680B0EEDAC5B1CE1AEA855D0.aspx?s=%22Lawrence+Pit%22
copy the source code over, change the GetChars function to this
static char [] GetChars (MemoryStream b, Encoding e)
{
return e.GetChars (b.ToArray(), 0, (int) b.Length);
}
I had to use the code snippet from here to properly hash encrypt the string
http://channel9.msdn.com/Forums/TechOff/Porting-to-WinRT/4df7586e1ef5400682eda00f0143b610
Use methods from the WebUtility class instead:
System.Net.WebUtility.UrlEncode(string);
System.Net.WebUtility.UrlDecode(string);

C# Equivalent to ifstream/ofstream in C++

I know this has probably been asked before, but I can't find it here on SO anywhere, and I can't get a clear answer on anything I look up on Google either.
I need to know the C# equivalent to C++'s ifstream/ofstream.
For instance, if I had the following C++ code:
ifstream input("myFile.txt");
ofstream output;
output.open("out.txt");
What would be the C# equivalent?
I found a site that said (for the in file portion, anyway) that the equivalent was this:
using System.IO;
FileStream fs = new FileStream("data.txt", FileMode.Open, FileAccess.Read);
I tried putting this in:
FileStream fs = new FileStream(input, FileAccess.Read);
I don't have the "FileMode" in mine because VS didn't recognize it. And "input" is a string in the parameters that holds the string value of the input file name (for example - "myFile.txt").
I know I've got to be missing something silly and minor, but I can't figure out what that is. Any help on this would be much appreciated!
I'm developing in VS2010, C#-4.0, WPF API.
FileStream is what you want. Take a look at the MSDN example on stream composition here.
I feel that StreamReader/StreamWriter offer similar functionality to c++'s ifstream/ofstream. FileStream is for dealing with byte[] data, whereas StreamReader/StreamWriter deal with text.
var writer = new StreamWriter(File.OpenWrite("myFile.txt");
writer.WriteLine("testing");
writer.Close();
var reader = new StreamReader(File.OpenRead("myFile.txt");
while ( !reader.EndOfStream )
{
var line = reader.ReadLine();
}

How do I use the registry?

In the simplest possible terms (I'm an occasional programmer who lacks up-to-date detailed programming knowledge) can someone explain the simplest way to make use of the registry in codegear C++ (2007).
I have a line of code in an old (OLD!) program I wrote which is causing a significant delay in startup...
DLB->Directory=pIniFile->ReadString("Options","Last Directory","no key!");
The code is making use of an ini file. I would like to be able to use the registry instead (to write variables such as the last directory the application was using)
But the specifics are not important. I'd just like a generic how-to about using the registry that's specific to codegear c++ builder.
I've googled this, but as usual with this type of thing I get lots of pages about c++ builder and a few pages about the windows registry, but no pages that explain how to use one with the other.
Use the TRegistry class... (include registry.hpp)
//Untested, but something like...
TRegistry *reg = new TRegistry;
reg->RootKey = HKEY_CURRENT_USER; // Or whatever root you want to use
reg->OpenKey("theKey",true);
reg->ReadString("theParam",defaultValue);
reg->CloseKey();
Note, opening and reading a ini file is usually pretty fast, so maybe you need to test your assumption that the reading of the ini is actually your problem, I don't think that just grabbing your directory name from the registry instead is going to fix your problem.
Include the Registry.hpp file:
#include <Registry.hpp>
Then in any function you have, you can write the following to read the value:
String __fastcall ReadRegistryString(const String &key, const String &name,
const String &def)
{
TRegistry *reg = new TRegistry();
String result;
try {
reg->RootKey = HKEY_CURRENT_USER;
if (reg->OpenKeyReadOnly(key)) {
result = reg->ReadString(name, def);
reg->CloseKey();
}
}
__finally {
delete reg;
}
return result;
}
So reading the value should be as easy as:
ShowMessage(ReadRegistryString("Options", "Last Directory", "none"));
You can use the following to write the value:
void __fastcall WriteRegistryString(const String &key, const String &name,
const String &value)
{
TRegistry *reg = new TRegistry();
try {
reg->RootKey = HKEY_CURRENT_USER;
if (reg->OpenKey(key, true)) {
reg->WriteString(name, value);
reg->CloseKey();
}
}
__finally {
delete reg;
}
}
Should be self explaining, remembering the try ... finally is actually really helpful when using the VCL TRegistry class.
Edit
I've heard that .ini files are stored in the registry in Windows, so if you want the speed advantage of ini files you should call them something else - like .cfg
This is something I've heard from an although reliable source, I haven't tested it myself.
Tim is right but an even simpler class to use is TIniRegFile but it is also more limited in what you can do.
Please see the documentation for the QSettings class from the Qt 4.5 library. It will allow you to load and store your program's configuration settings easily and in a cross-platform manner. The Windows implementation uses the Windows registry for loading and storing your program's configuration data. On other platforms, the platform's preferred, native mechanism for storing configuration data will be used. This is far better than interacting with the Windows registry directly, as you will not be tied to a specific platform.