Feeds:
Posts
Comments

httppost

hmm sometimes its necessary to send parameters to an url from a desktop application ..and here is the code to do that using httppost method….

code for class file

using System.IO;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace Emri
{
class PostSubmit
{
public enum PostTypeEnum
{
/// <summary>
/// Does a get against the source.
/// </summary>
Get,
/// <summary>
/// Does a post against the source.
/// </summary>
Post
}

private string m_url = string.Empty;
private NameValueCollection m_values = new NameValueCollection();
private PostTypeEnum m_type = PostTypeEnum.Get;
/// <summary>
/// Default constructor.
/// </summary>
public PostSubmitter()
{
}

/// <summary>
/// Constructor that accepts a url as a parameter
/// </summary>
/// <param name=”url”>The url where the post will be submitted to.</param>
public PostSubmitter(string url)
: this()
{
m_url = url;
}

/// <summary>
/// Constructor allowing the setting of the url and items to post.
/// </summary>
/// <param name=”url”>the url for the post.</param>
/// <param name=”values”>The values for the post.</param>
public PostSubmitter(string url, NameValueCollection values)
: this(url)
{
m_values = values;
}

/// <summary>
/// Gets or sets the url to submit the post to.
/// </summary>
public string Url
{
get
{
return m_url;
}
set
{
m_url = value;
}
}
/// <summary>
/// Gets or sets the name value collection of items to post.
/// </summary>
public NameValueCollection PostItems
{
get
{
return m_values;
}
set
{
m_values = value;
}
}
/// <summary>
/// Gets or sets the type of action to perform against the url.
/// </summary>
public PostTypeEnum Type
{
get
{
return m_type;
}
set
{
m_type = value;
}
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <returns>a string containing the result of the post.</returns>
public string Post()
{
StringBuilder parameters = new StringBuilder();
for (int i = 0; i < m_values.Count; i++)
{
EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
}
string result = PostData(m_url, parameters.ToString());
return result;
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <param name=”url”>The url to post to.</param>
/// <returns>a string containing the result of the post.</returns>
public string Post(string url)
{
m_url = url;
return this.Post();
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <param name=”url”>The url to post to.</param>
/// <param name=”values”>The values to post.</param>
/// <returns>a string containing the result of the post.</returns>
public string Post(string url, NameValueCollection values)
{
m_values = values;
return this.Post(url);
}
/// <summary>
/// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
/// </summary>
/// <param name=”postData”>The data to post.</param>
/// <param name=”url”>the url to post to.</param>
/// <returns>Returns the result of the post.</returns>
private string PostData(string url, string postData)
{
HttpWebRequest request = null;
if (m_type == PostTypeEnum.Post)
{
Uri uri = new Uri(url);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = “POST”;
request.ContentType = “application/x-www-form-urlencoded”;
request.ContentLength = postData.Length;
using (Stream writeStream = request.GetRequestStream())

{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
}
}
else
{
Uri uri = new Uri(url + “?” + postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = “GET”;
}
string result = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
return result;
}
/// <summary>
/// Encodes an item and ads it to the string.
/// </summary>
/// <param name=”baseRequest”>The previously encoded data.</param>
/// <param name=”dataItem”>The data to encode.</param>
/// <returns>A string containing the old data and the previously encoded data.</returns>
private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
{
if (baseRequest == null)
{
baseRequest = new StringBuilder();
}
if (baseRequest.Length != 0)
{
baseRequest.Append(”&”);
}
baseRequest.Append(key);
baseRequest.Append(”=”);
baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
}

dont forget to add reference system.web in this form…

in the form

PostSubmit post = new PostSubmit();
post.Url = “http://www.xx.com/xservice/Service.aspx”;
post.PostItems.Add(”MsgPhoneNo”, mob);
post.PostItems.Add(”MsgText”, mesg);
post.PostItems.Add(”MsgSendTime”, System.DateTime.Now.ToString());

post.Type = PostSubmit.PostTypeEnum.Post;
string result = post.Post();

in the webpage u can receive the values like this

MsgPhoneNo = Request.Form["MsgPhoneNo"].ToString();

httppost

hmm sometimes its necessary to send parameters to an url from a desktop application ..and here is the code to do that using httppost method….

code for class file

using System.IO;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace Emri
{
class PostSubmit
{
public enum PostTypeEnum
{
/// <summary>
/// Does a get against the source.
/// </summary>
Get,
/// <summary>
/// Does a post against the source.
/// </summary>
Post
}

private string m_url = string.Empty;
private NameValueCollection m_values = new NameValueCollection();
private PostTypeEnum m_type = PostTypeEnum.Get;
/// <summary>
/// Default constructor.
/// </summary>
public PostSubmitter()
{
}

/// <summary>
/// Constructor that accepts a url as a parameter
/// </summary>
/// <param name=”url”>The url where the post will be submitted to.</param>
public PostSubmitter(string url)
: this()
{
m_url = url;
}

/// <summary>
/// Constructor allowing the setting of the url and items to post.
/// </summary>
/// <param name=”url”>the url for the post.</param>
/// <param name=”values”>The values for the post.</param>
public PostSubmitter(string url, NameValueCollection values)
: this(url)
{
m_values = values;
}

/// <summary>
/// Gets or sets the url to submit the post to.
/// </summary>
public string Url
{
get
{
return m_url;
}
set
{
m_url = value;
}
}
/// <summary>
/// Gets or sets the name value collection of items to post.
/// </summary>
public NameValueCollection PostItems
{
get
{
return m_values;
}
set
{
m_values = value;
}
}
/// <summary>
/// Gets or sets the type of action to perform against the url.
/// </summary>
public PostTypeEnum Type
{
get
{
return m_type;
}
set
{
m_type = value;
}
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <returns>a string containing the result of the post.</returns>
public string Post()
{
StringBuilder parameters = new StringBuilder();
for (int i = 0; i < m_values.Count; i++)
{
EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
}
string result = PostData(m_url, parameters.ToString());
return result;
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <param name=”url”>The url to post to.</param>
/// <returns>a string containing the result of the post.</returns>
public string Post(string url)
{
m_url = url;
return this.Post();
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <param name=”url”>The url to post to.</param>
/// <param name=”values”>The values to post.</param>
/// <returns>a string containing the result of the post.</returns>
public string Post(string url, NameValueCollection values)
{
m_values = values;
return this.Post(url);
}
/// <summary>
/// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
/// </summary>
/// <param name=”postData”>The data to post.</param>
/// <param name=”url”>the url to post to.</param>
/// <returns>Returns the result of the post.</returns>
private string PostData(string url, string postData)
{
HttpWebRequest request = null;
if (m_type == PostTypeEnum.Post)
{
Uri uri = new Uri(url);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = “POST”;
request.ContentType = “application/x-www-form-urlencoded”;
request.ContentLength = postData.Length;
using (Stream writeStream = request.GetRequestStream())

{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
}
}
else
{
Uri uri = new Uri(url + “?” + postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = “GET”;
}
string result = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
return result;
}
/// <summary>
/// Encodes an item and ads it to the string.
/// </summary>
/// <param name=”baseRequest”>The previously encoded data.</param>
/// <param name=”dataItem”>The data to encode.</param>
/// <returns>A string containing the old data and the previously encoded data.</returns>
private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
{
if (baseRequest == null)
{
baseRequest = new StringBuilder();
}
if (baseRequest.Length != 0)
{
baseRequest.Append(“&”);
}
baseRequest.Append(key);
baseRequest.Append(“=”);
baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
}

dont forget to add reference system.web in this form…

in the form

PostSubmit post = new PostSubmit();
post.Url = “http://www.xx.com/xservice/Service.aspx”;
post.PostItems.Add(“MsgPhoneNo”, mob);
post.PostItems.Add(“MsgText”, mesg);
post.PostItems.Add(“MsgSendTime”, System.DateTime.Now.ToString());

post.Type = PostSubmit.PostTypeEnum.Post;
string result = post.Post();

in the webpage u can receive the values like this

MsgPhoneNo = Request.Form["MsgPhoneNo"].ToString();

Datalist Paging

hmm the code for datalist paging is as follows

public void binddata()
{
//uid = “dharma.sonu@gmail.com1″;
uid = Session["user"].ToString() + “1″;
qid = Request.QueryString["qid"].ToString();
string query = “select * from albumpics where userid=’” + uid + “‘ and albumname=’” + qid + “‘”;
SqlDataAdapter da = new SqlDataAdapter(query, con);
con.Close();
con.Open();
DataSet ds = new DataSet();
int startRecord = (int.Parse(CurrentPage.Value) – 1) *
int.Parse(PageSize.Value);
da.Fill(ds, startRecord, int.Parse(PageSize.Value), “albumpics”);
if (ds.Tables[0].Rows.Count > 0)
{
DataList1.DataSource = ds;
DataList1.DataBind();
}

//Second Part
//Create a new Command to select the total number of records
SqlCommand myCmd = new SqlCommand(“SELECT Count(*) from albumpics where userid=’” + uid + “‘ and albumname=’” + qid + “‘”, con);
con.Close();
con.Open();
//retrieve the value
TotalSize.Value = myCmd.ExecuteScalar().ToString();

BuildPagers();
}
public void Page_DataList(object sender, EventArgs e)
{
//Check for Button clicked
if (((LinkButton)sender).ID == “Prev”)
{
//Check if we are on any page greater than 0
if ((int.Parse(CurrentPage.Value) – 1) >= 0)
{
//Decrease the CurrentPage Value
CurrentPage.Value = (int.Parse(CurrentPage.Value) – 1).ToString();
}
}
else if (((LinkButton)sender).ID == “Next”)
{
//Check if we can display the next page.
if ((int.Parse(CurrentPage.Value) * int.Parse(PageSize.Value))
< int.Parse(TotalSize.Value))
{
//Increment the CurrentPage value
CurrentPage.Value = (int.Parse(CurrentPage.Value) + 1).ToString();
}
}
//Rebuild the Grid
binddata();
}

public void BuildPagers()
{
//Check if its possible to have the previous page
if ((int.Parse(CurrentPage.Value) – 1) <= 0)
{
Prev.Enabled = false;
}
else
{
Prev.Enabled = true;
}
//Check if its possible to have the next page
if ((int.Parse(CurrentPage.Value) * int.Parse(PageSize.Value))
>= int.Parse(TotalSize.Value))
{
Next.Enabled = false;
}
else
{
Next.Enabled = true;
}
}

<asp:DataList ID=”DataList1″ runat=”server” RepeatColumns=”3″>
<ItemTemplate>
<table id=”Table3″ cellSpacing=”1″ cellPadding=”2″  border=”0″>
<tr>
<td  >

<asp:ImageButton ID=”ImageButton1″ runat=”server” ImageUrl=’<%# getpath(DataBinder.Eval(Container.DataItem,”imgid”).ToString())%>’ OnClick=”ImageButton1_Click” />
</td>
<td>
<table >
<asp:Panel ID=”Panel1″ runat=”server” Visible=”false” >

<tr>
<td>
<asp:Label ID=”Label2″ runat=”server” Text=”Caption :”></asp:Label><asp:TextBox ID=”txtcap”
runat=”server” TextMode=”MultiLine” Text=’<%# DataBinder.Eval(Container.DataItem,”Description”).ToString()%>’></asp:TextBox></td>
</tr>
<tr>
<td align=”left”>
<asp:RadioButton ID=”RBcover” runat=”server” TextAlign =”right” Text =”Album Cover”/></td>
</tr>
</asp:Panel>

<tr>
<td align=”left” >
<table >
<tr>
<td align=”center” >
<asp:LinkButton ID=”LinkButton1″ runat=”server” OnClick =”LinkButton1_Click2″>Edit</asp:LinkButton></td> <td>
<asp:LinkButton ID=”LinkButton2″ runat=”server” OnClick =”LinkButton2_Click1″>Delete</asp:LinkButton></td>
</tr>
</table>

</td>
</tr>
</table>
</td></tr>
<tr>
<td>
<asp:Label ID=”Label1″ runat=”server” Text=’<%# DataBinder.Eval(Container.DataItem,”Description”).ToString()%>’ ></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID=”lblimgid” runat=”server” Text=’<%# DataBinder.Eval(Container.DataItem,”imgid”).ToString()%>’ Visible =”false” ></asp:Label>
</td>
</tr>
</table>
</ItemTemplate></asp:DataList>

hmm while am working on maps , i got a problem to find the distance between two latitudes and longitudes ..here is the code for that ..here unit =k is distance in kilometers and n is nautical miles…

private double distance(double lat1, double lon1, double lat2, double lon2, char unit)

    {

        double theta = lon1 – lon2;

        double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));

        dist = Math.Acos(dist);

        dist = rad2deg(dist);

        dist = dist * 60 * 1.1515;

        if (unit == ‘K’)

        {

            dist = dist * 1.609344;

        }

        else if (unit == ‘N’)

        {

            dist = dist * 0.8684;

        }

        return (dist);

    }

    private double deg2rad(double deg)

    {

        return (deg * Math.PI / 180.0);

    }

    private double rad2deg(double rad)

    {

        return (rad / Math.PI * 180.0);

    }

Hello world!

About Me

Am dharma presently working as a Software Trainee in VirtuMobile Technologies Hyderabad.I completed my B-tech in 2006 from Vidyanikethan Engineering College Tirupati,Presently am working on Microsoft Technologies C#,Asp.net.Interested to learn new technologies.