Search This Blog

Thursday, July 2, 2009

Read Child Node From a XML and Bind a DropDown List By this

My xml file is Like that

Root Node := Weather_Data
Parent Nade : = Weather

Child Node Under Parent "Weather"
ChildNode : City
ChildNode :Weather

public ArrayList ReadValue(string tag, string key)
{
string _file = "Weather.xml";
ArrayList arr = new ArrayList();
string _path = Server.MapPath("~/");
//load the document into a stream
FileStream stream = new FileStream(_path + _file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument doc = new XmlDocument();
//load the stream into an XML Document
doc.Load(stream);
//variable to hold the value
string returnValue = string.Empty;
//get all the elements for the specified tag
XmlNodeList nodeList = doc.GetElementsByTagName(tag);
//loop through the document
for (int i = 0; i < nodeList.Count; i++)
{
for (int j = 0; j < nodeList[i].ChildNodes.Count; j++)
{
//check to see if we have a match
if (nodeList[i].ChildNodes[j].Name == key)
{
returnValue = nodeList[i].ChildNodes[j].InnerText;
arr.Add(nodeList[i].ChildNodes[j].InnerText);
//break;
}
}
}
return arr;
}

Call the above method for bind your DropdownList Like

ArrayList arrList= ReadValue("Weather", "City");
DropDownList1.DataSource =
arrList;
DropDownList1.DataBind();



Hope This will help you

Wednesday, July 1, 2009

HTTPWeb Request and Responses With Source Code

HTTP REQUEST /RESPONSES

Basically, an HTTP client initiates a request through web browser. It establishes a Transmission Control Protocol (TCP) connection to a particular port on a host server (port 80 by default). An HTTP server that port waits for the client's request message. After receiving the request, the server sends back a status line, such as "HTTP/1.1 200 OK", and a message of its own, the body of which is may be the requested resource, an error message, or some other information.

Resource accessing by the HTTP are identified using Uniform Resource Identifier (URI)or, more specifically, Uniform Resource Locator (URL) by the http: or https URI schemas.

HTTP REQUEST STYLE

1.
Request line (such as GET)
2.Header ( such as Accept-Language: en)
3.Empty Line
4.Optional message body


HTTP REQUEST METHODS

Hyper Text Transfer Protocol (HTTP) defines eight methods for the next action for existing resource.I am giving you a short description on that eight methods.


HEAD
It is almost like a get request without the response body. This is useful for retrieving meta-information written in response headers, without sending the entire content.

GET
Get requests a representation of the specified resource.

POST
Post submits data to be processed from your form to the requesting resources. The data is included in the body of the request. It may create a new resource or update the existing resources or both.

PUT
Put uploads a representation of the specified resource.

DELETE
It deletes the specified resource.

TRACE
It returns back to the received request, so that a client can see what intermediate servers are adding or changing in existing the request.

OPTIONS
It returns the HTTP methods that the server supports for specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource.

CONNECT
Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy.

CREATING REQUEST AND GETTING RESPONSES

// Create a request for the URL.
WebRequest request = WebRequest.Create("http://www.google.com");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
if (response.StatusDescription == "OK")
{
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
}


CREATING REQUEST AND GETTING RESPONSES WITH AUTHENTICATION
 strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;

ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
// Write the request back IPN strings
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
stOut.Write(strNewValue);
stOut.Close();

//send the request, read the response
HttpWebResponse strResponse = (HttpWebResponse)req.GetResponse();
Stream IPNResponseStream = strResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(IPNResponseStream, encode);

char[] read = new char[257];
// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
readStream.Close();
strResponse.Close();


Check URL Exsists Or Not(HttpWebResponse Status)

Import the base class "System.Net" at the begining

string URL = TextBox1.Text;
try
{
//Create a HTTP Web Request for the URL.
WebRequest request = WebRequest.Create(URL);
request.Proxy = null;
//Get the HTTP Web Response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Display the status.
Response.Write(response.StatusDescription);

if (response.StatusDescription == "OK")
{
Response.Write("\n Site Exists.");
}
else
{
Response.Write("Does Not Exists.");
}
}
catch (Exception ex)
{
throw new Exception("Request Incomplete..Please Contact with your System Administrator :" + ex);
}