Categories

Category Specific RSS

Is Internet Connection Available in C sharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace Common {
public static class MailHelper {

public static bool InternetConnectionAvailable( ) {
bool result = false;
Ping ping = new Ping( );
PingReply reply = null;
try {
reply = ping.Send( “www.google.com” );
result = reply.Status == IPStatus.Success;
} catch ( Exception ) {

}

return result;
}

}
}

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

Validate Recaptcha in C sharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Mvc;

namespace WeBlog.Core {
public static class RecaptchaUtils {
public static JsonResult ValidateRecaptcha( string privateKey, string remoteip, string challenge, string response) {
string postData = String.Format(“privatekey={0}&remoteip={1}&challenge={2}&response={3}”,
privateKey, remoteip,
challenge, response);
JsonResult result = new JsonResult();
byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData);

Uri serviceUri = new Uri(“http://api-verify.recaptcha.net/verify”, UriKind.Absolute);
try {
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
webRequest.ContentType = “application/x-www-form-urlencoded”;
webRequest.ContentLength = postDataBuffer.Length;
webRequest.Method = “POST”;

//incase you are using a proxy server
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Proxy = proxy;

Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(postDataBuffer, 0, postDataBuffer.Length);

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;

using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
jsonResponse = sr.ReadToEnd();

string[] tokens = jsonResponse.Split(new char[] { ‘\n’ });
if (tokens.Length == 2) {
Boolean success = tokens[0].Equals(“true”, StringComparison.CurrentCulture);
result.Data = new { Success = success, Message = tokens[1] };
}
else {
result.Data = new { Success = false, Message = “Invalid response returned” };
}
}
catch (Exception ex) {
result.Data = new { Success = false, Message = ex.Message };
}

return result;
}
}
}

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

A basic web client in C sharp

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

public class MainClass {
public static void Main() {
System.Net.WebClient Client = new WebClient();
Stream strm = Client.OpenRead(“http://www.google.com”);
StreamReader sr = new StreamReader(strm);
string line;
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
strm.Close();
}
}

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

Append a string to a file in PERL

open OUT, “>>”, “filename” or die “Couldn’t open file: $!\n”;
print OUT “aString”;
close OUT;

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

Append a string to a file in Java

try {
BufferedWriter out = new BufferedWriter(new FileWriter(“filename”, true));
out.write(“aString”);
out.close();
} catch (IOException e) {
}

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare