How to call web service in .net

Leave a comment

Today I have worked on calling web service from console application, and I have searched on google also, but it seems to be not much.

I have done with the following code to call web service.

Imports System.Net
Imports System.IO
Imports System.Text

Public Class ScheduleTrigger

    Public Shared Sub Main()
        Const Template As String = _
"<?xml version=""1.0"" encoding=""utf-8""?>" & vbCrLf & _
"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & vbCrLf & _
"  <soap:Body>" & vbCrLf & _
"    <RunResponse xmlns=""http://tempuri.org/"" />" & vbCrLf & _
"  </soap:Body>" & vbCrLf & _
"</soap:Envelope>"

        Dim url = New Uri("http://localhost/Schedule.asmx")

        Dim request As HttpWebRequest = WebRequest.Create(url)
        request.Method = "POST"
        request.ContentType = "text/xml; charset=utf-8"
        request.Headers.Add("SOAPAction", "http://tempuri.org/Run")

        Using stream = New MemoryStream()
            Using writer = New StreamWriter(stream, Encoding.UTF8)
                writer.Write(Template)
            End Using
            Dim bytes As Byte() = stream.ToArray()
            request.ContentLength = bytes.Length
            Using output = request.GetRequestStream()
                output.Write(bytes, 0, bytes.Length)
            End Using
        End Using

        Using response As HttpWebResponse = request.GetResponse()
            If response.StatusCode = HttpStatusCode.OK Then
                Console.Write("Success ")
                Console.WriteLine("Please Press Any Key to Exit!")
                Console.Read()
            End If

        End Using
    End Sub

End Class

JKhmerConverter

Leave a comment

JKhmerConverter
is able to convert legacy Khmer font to Khmer Unicode, and Khmer Unicode to legacy Khmer font.
I build this project in order to contribute with open source community in Cambodia, and sharevisionteam.
This project is converted from http://unicode.codeplex.com that built in C# 3.5 t, and due to there is no Khmer Unicode Converter, I have spent my free time to build it up and it’s developed in Java 6.

You can find the binary file and source code file at google code JKhmerConverter
And click here to download download JKhmerConverter

I’m so happy with my first contribute and I hope that it’s my help to society in IT.

Snippet code as below:



/**
 *
 * @author LEE
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String str = "eRs‘og";
        LegacyToUnicode legacyToUnicode = new LegacyToUnicode();
        System.out.println(legacyToUnicode.convert(str, "Limon S1"));
    }
}

Automatically login to DNN after registration

Leave a comment

Public Sub AutomaticLogin(ByVal userName As String)
Dim objUserInfo As UserInfo = UserController.GetUserByName(PortalId, userName)
UserController.UserLogin(PortalId, objUserInfo, PortalSettings.PortalName, Request.UserHostAddress, False)
Response.Redirect("/Home.aspx")
End Sub
Public Sub AutomaticLogin(ByVal userName As String)

Dim objUserInfo As UserInfo = UserController.GetUserByName(PortalId, userName)

UserController.UserLogin(PortalId, objUserInfo, PortalSettings.PortalName, Request.UserHostAddress, False)

Response.Redirect("/Home.aspx")

End Sub

iPad for every one

Leave a comment

Send email from CSharp with Gmail Account

Leave a comment

string gMailAccount = "chem.leakhina@gmail.com";
            string password = "leakhina*147";
            string to = "chem.leakhina@gmail.com";
            NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(gMailAccount);
            msg.To.Add(new MailAddress(to));
            msg.Subject = "subject";
            msg.Body = "message";
            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = loginInfo;
            client.Send(msg);

Fix error when IIS install after .Net Framework

Leave a comment

Run this command : %Windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i

Generate Color Ramp in Javascript

Leave a comment

  var theColorBegin = 0x632593;
  var theColorEnd = 0xFF7766;
  var theNumSteps = 5;

  theR0 = (theColorBegin & 0xff0000) >> 16;

  theG0 = (theColorBegin & 0x00ff00) >> 8;

  theB0 = (theColorBegin & 0x0000ff) >> 0;

  theR1 = (theColorEnd & 0xff0000) >> 16;

  theG1 = (theColorEnd & 0x00ff00) >> 8;

  theB1 = (theColorEnd & 0x0000ff) >> 0;

  // return the interpolated value between pBegin and pEnd

  function interpolate(pBegin, pEnd, pStep, pMax) {

    if (pBegin < pEnd) {

      return ((pEnd - pBegin) * (pStep / pMax)) + pBegin;

    } else {

      return ((pBegin - pEnd) * (1 - (pStep / pMax))) + pEnd;

    }

  }

  function generateColor(){
    for (i = 0; i <= theNumSteps; i++) {

      theR = interpolate(theR0, theR1, i, theNumSteps);

      theG = interpolate(theG0, theG1, i, theNumSteps);

      theB = interpolate(theB0, theB1, i, theNumSteps);

      theVal = ((( theR << 8 ) |  theG ) << 8 ) | theB;
      console.log(theVal.toString(16));
      }
  }

The result will display as below:
632593
82358a
a14581
c05678
df666f
ff7766

Barcamp in Phnom Pen

Leave a comment

You are invited to join barcamp in Phnom pen on 3-4 October 2009 — 7:30AM – 5:00PM.
Barcamp Site
Barcamp Phnom Pen

Ubuntu Linux Install Sun Java Development Kit ( JDK ) and Java Runtime Environment ( JRE )

Leave a comment

Question :
How do I install Sun Java Development Kit (JDK) and Java Runtime Environment (JRE) under Ubuntu Linux? It appears that there are multiple JRE installed by default under Ubuntu. How do I select and use Sun JRE only? Can you explain steps required to set the environment to run java programs or apps?

Answer:
Ubuntu Linux 7.10 has following packages from Sun:
=> sun-java6-bin : Sun Java Runtime Environment (JRE) 6

=> sun-java6-demo : Sun Java Development Kit (JDK) 6 demos

=> sun-java6-jdk : Sun Java Development Kit (JDK) 6

=> sun-java6-jre : Sun Java Runtime Environment (JRE) 6

Open a shell prompt (terminal) and type the following to install JDK and JRE:

$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk

Setup the default Java version

Ubuntu Linux comes with update-java-alternatives utility to updates all alternatives belonging to one runtime or development kit for the Java language. To select, Sun’s JVM as provided in Ubuntu 7.10, enter:

$ sudo update-java-alternatives -s java-6-sun

You also need to edit a file called /etc/jvm. This file defines the default system JVM search order. Each JVM should list their JAVA_HOME compatible directory in this file. The default system JVM is the first one available from top to bottom. Open /etc/jvm

$ sudo vi /etc/jvm

Make sure /usr/lib/jvm/java-6-sun is added to the top of JVM list

/usr/lib/jvm/java-6-sun

At the end your file should read as follows:

/usr/lib/jvm/java-6-sun
/usr/lib/jvm/java-gcj
/usr/lib/jvm/ia32-java-1.5.0-sun
/usr/lib/jvm/java-1.5.0-sun
/usr

Save and close the file.

Setup the environment variable

You also need to setup JAVA_HOME and PATH variable. Open your $HOME/.bash_profile or /etc/profile (system wide) configuration. Open your .bash_profile file:

$ vi $HOME/.bash_profile

Append following line:

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin

Save and close the file.

Test your new JDK
Type the following command to display version:

$ java -version

Output:

java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Server VM (build 1.6.0_03-b05, mixed mode)

Try HelloWorld.java – first java program

$ vi HelloWorld.java

Append code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Save and close the file. Compile HelloWorld.java and execute program by typing following two instructions:

$ javac HelloWorld.java
$ java HelloWorld

Output:

Hello, World!

JMF Webcam app with saving JPEG

4 Comments

Since so many people ask for this code, but never get it, I figured I’d repost it. I didn’t write it, but I added the jpeg saving part, and modified it for my device, which you will have to do to get it to work. Just go into JMFRegistry and get the device name from your webcam and then edit the code. Also, keep in mind that some webcams don’t work with JMF. You have to have a webcam that supports VFW or WDM interface.
And here’s the code:

import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import javax.media.protocol.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
 
public class SwingCapture extends Panel implements ActionListener 
{
  public static Player player = null;
  public CaptureDeviceInfo di = null;
  public MediaLocator ml = null;
  public JButton capture = null;
  public Buffer buf = null;
  public Image img = null;
  public VideoFormat vf = null;
  public BufferToImage btoi = null;
  public ImagePanel imgpanel = null;
  
  public SwingCapture() 
  {
    setLayout(new BorderLayout());
    setSize(320,550);
    
    imgpanel = new ImagePanel();
    capture = new JButton("Capture");
    capture.addActionListener(this);
    
    String str1 = "vfw:Logitech USB Video Camera:0";
    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    di = CaptureDeviceManager.getDevice(str2);
    ml = di.getLocator();
    
    try 
    {
      player = Manager.createRealizedPlayer(ml);
      player.start();
      Component comp;
      
      if ((comp = player.getVisualComponent()) != null)
      {
        add(comp,BorderLayout.NORTH);
      }
      add(capture,BorderLayout.CENTER);
      add(imgpanel,BorderLayout.SOUTH);
    } 
    catch (Exception e) 
    {
      e.printStackTrace();
    }
  }
 
 
  
  public static void main(String[] args) 
  {
    Frame f = new Frame("SwingCapture");
    SwingCapture cf = new SwingCapture();
    
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      playerclose();
      System.exit(0);}});
    
    f.add("Center",cf);
    f.pack();
    f.setSize(new Dimension(320,550));
    f.setVisible(true);
  }
  
  
  public static void playerclose() 
  {
    player.close();
    player.deallocate();
  }
  
 
  public void actionPerformed(ActionEvent e) 
  {
    JComponent c = (JComponent) e.getSource();
    
    if (c == capture) 
    {
      // Grab a frame
      FrameGrabbingControl fgc = (FrameGrabbingControl)
      player.getControl("javax.media.control.FrameGrabbingControl");
      buf = fgc.grabFrame();
      
      // Convert it to an image
      btoi = new BufferToImage((VideoFormat)buf.getFormat());
      img = btoi.createImage(buf);
      
      // show the image
      imgpanel.setImage(img);
      
      // save image
      saveJPG(img,"c:\\test.jpg");
    }
  }
  
  class ImagePanel extends Panel 
  {
    public Image myimg = null;
    
    public ImagePanel() 
    {
      setLayout(null);
      setSize(320,240);
    }
    
    public void setImage(Image img) 
    {
      this.myimg = img;
      repaint();
    }
    
    public void paint(Graphics g) 
    {
      if (myimg != null) 
      {
        g.drawImage(myimg, 0, 0, this);
      }
    }
  }
  
 
  public static void saveJPG(Image img, String s)
  {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.drawImage(img, null, null);
 
    FileOutputStream out = null;
    try
    { 
      out = new FileOutputStream(s); 
    }
    catch (java.io.FileNotFoundException io)
    { 
      System.out.println("File Not Found"); 
    }
    
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
    param.setQuality(0.5f,false);
    encoder.setJPEGEncodeParam(param);
    
    try 
    { 
      encoder.encode(bi); 
      out.close(); 
    }
    catch (java.io.IOException io) 
    {
      System.out.println("IOException"); 
    }
  }
  
}

More detail

Older Entries