using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;
public partial class QueryModel : System.Web.UI.Page
{
private TimeSpan ts = new TimeSpan();
protected void Page_Load(object sender, EventArgs e)
{
RunSample();
}
public void RunSample()
{
// http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=finances&format=pdf
StringBuilder strURL = new StringBuilder();
string strWebServie = "http://search.yahooapis.com/WebSearchService/V1/webSearch?";
string appid = "sE2WAFHV34FQU6k_0.eQ5Ng8IXnc.VgAlA7Ej_F0P6T5tY48kftoUHiYTRq2RgA-";
string strUri = string.Format("{0}appid={1}&query={2}", strWebServie, appid, txtKeyword.Text);
PrintSource(new Uri(strUri));
// PrintSource(new Uri("http://developer.yahoo.com"));
}
public static void PrintSource(Uri address)
{
HttpWebRequest request;
HttpWebResponse response = null;
StreamReader reader;
StringBuilder sbSource;
if (address == null) { throw new ArgumentNullException("address"); }
try
{
// Create and initialize the web request
request = WebRequest.Create(address) as HttpWebRequest;
request.UserAgent = ".NET Sample";
request.KeepAlive = false;
// Set timeout to 15 seconds
request.Timeout = 15 * 1000;
// Get response
response = request.GetResponse() as HttpWebResponse;
if (request.HaveResponse == true && response != null)
{
// Get the response stream
reader = new StreamReader(response.GetResponseStream());
// Read it into a StringBuilder
sbSource = new StringBuilder(reader.ReadToEnd());
// Console application output
Console.WriteLine(sbSource.ToString());
}
}
catch (WebException wex)
{
// This exception will be raised if the server didn't return 200 - OK
// Try to retrieve more information about the network error
if (wex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
{
Console.WriteLine("The server returned '{0}' with the status code {1} ({2:d}).",
errorResponse.StatusDescription, errorResponse.StatusCode,
errorResponse.StatusCode);
}
}
}
finally
{
if (response != null) { response.Close(); }
}
}
public void PrintDataSet(DataSet ds)
{
//StringBuilder strResult= new StringBuilder();
//if (ds.Tables["Result"].Rows.Count > 0)
//{
// int i = 1;
// int j = 0;
// strResult.Append("Search Result :");
// strResult.Append("
");
// strResult.Append("");
// foreach (DataRow dr in ds.Tables["Result"].Rows)
// {
// strResult.Append(string.Format("| {0}.{2} |
", i.ToString(), dr["ClickUrl"].ToString(), dr["Title"].ToString()));
// strResult.Append("");
// strResult.Append("| ");
// strResult.Append(dr["Summary"].ToString());
// strResult.Append(" | ");
// strResult.Append("
");
// strResult.Append(string.Format("| {0} |
", dr["Url"].ToString()));
// strResult.Append("
|
");
// i++;
// }
// strResult.Append("
");
//}
//lblResult.Text = strResult.ToString();
//plQueryModel.Visible = false;
//plResult.Visible = true;
Response.Write(string.Format("Result time:{0}", DateTime.Now.ToLongTimeString()));
if (ds.Tables["Result"].Rows.Count > 0)
{
int i = 1;
int j = 0;
Response.Write("Search Result :");
Response.Write("
");
Response.Write("");
foreach (DataRow dr in ds.Tables["Result"].Rows)
{
Response.Write(string.Format("| {0}.{2} |
", i.ToString(), dr["ClickUrl"].ToString(), dr["Title"].ToString()));
Response.Write("");
Response.Write("| ");
Response.Write(dr["Summary"].ToString());
Response.Write(" | ");
Response.Write("
");
Response.Write(string.Format("| {0} |
", dr["Url"].ToString()));
Response.Write("
|
");
i++;
}
Response.Write("
");
}
//// Print out all tables and their columns
//foreach (DataTable table in ds.Tables)
//{
// Response.Write(string.Format("TABLE '{0}'", table.TableName));
// Response.Write("
");
// Response.Write(string.Format("Total # of rows: {0}", table.Rows.Count));
// Response.Write("
");
// Response.Write("---------------------------------------------------------------");
// Response.Write("
");
// foreach (DataColumn column in table.Columns)
// {
// Response.Write(string.Format("- {0} ({1})", column.ColumnName, column.DataType.ToString()));
// Response.Write("
");
// } // foreach column
// Response.Write(System.Environment.NewLine);
// Response.Write("
");
//} // foreach table
//// Print out table relations
//foreach (DataRelation relation in ds.Relations)
//{
// Response.Write(string.Format("RELATION: {0}", relation.RelationName));
// Response.Write("
");
// Response.Write("---------------------------------------------------------------");
// Response.Write("
");
// Response.Write(string.Format("Parent: {0}", relation.ParentTable.TableName));
// Response.Write("
");
// Response.Write(string.Format("Child: {0}", relation.ChildTable.TableName));
// Response.Write("
");
// Response.Write(System.Environment.NewLine);
// Response.Write("
");
//} // foreach relation
}
protected void Button1_Click(object sender, EventArgs e)
{
// RunSample();
StringBuilder strURL = new StringBuilder();
string strWebServie = "http://search.yahooapis.com/WebSearchService/V1/webSearch?";
//string appid = "sE2WAFHV34FQU6k_0.eQ5Ng8IXnc.VgAlA7Ej_F0P6T5tY48kftoUHiYTRq2RgA-";
//string appid = "14IaD83V34EW_xdWCiNNJ9.Pgfs1O0ejBD6p13svxwQVpYpBPbIvRjwcFZEwXwE-";
string appid = ConfigurationManager.AppSettings["appid"].ToString();
string query = "";
string queryoption = "";
string term = "";
string siteurl = "";
string queryterm = "";
string resultnum = ConfigurationManager.AppSettings["resultnum"].ToString();
string strUri = string.Format("{0}appid={1}&results={2}", strWebServie, appid,resultnum);
string sPagemodel="";
DateTime dtBegin, dtEnd;
// Search Meta Words
//Here is a list of special keywords that give you results unique to that special keyword instruction. You can enter these Meta words directly into the Yahoo! Search box.
//site: use to find all documents within a particular domain and all it's subdomains.
//Example: site:yahoo.com
//hostname: use to find all documents from a particular host only.
//Example: hostname:autos.yahoo.co
//link: use to find documents that link to a particular url.
//Example: link:http://autos.yahoo.com/
//url: use to find a specific document in our index.
//Example: url:http://edit.autos.yahoo.com/repair/tree/0.html
//inurl: use to find a specific keyword as part of indexed urls.
//Example: inurl:bulgarian
//intitle: use to find a specific keyword as part of the indexed titles.
//Example: intitle:Bulgarian
Session["querytype"] = "";
Session["singlepage"] = "";
Session["multipage"] = "";
Session["linkstructure"] = "";
Session["similarity"] = "";
Session["synonym"] = "";
// if choose topic distillation
//預設的選項都是topic distillation
//有2個狀況,1:page model選擇title,若選擇其他沒有用處
if (cblstQueryType.SelectedValue.Equals("TD"))
{
Session["querytype"] = "Query Type"+"-"+cblstQueryType.SelectedItem.Text;
if (cblstSPage.SelectedValue.Trim().Equals("Title"))//if choose topic distillation ,then must choose title option
{
queryterm = string.Format("intitle:{0}", txtKeyword.Text);
Session["singlepage"] = "Single Page"+"-"+cblstSPage.SelectedItem.Text;
}
else
{
foreach (ListItem item in cblstSPage.Items)
{
if (item.Selected)
sPagemodel += cblstSPage.SelectedItem.Text + ",";
}
if (!sPagemodel.Trim().Equals(""))
{
int pos = sPagemodel.Length - 1;
sPagemodel = sPagemodel.Substring(0, pos);
Session["singlepage"] = "Single Page" + "-" + sPagemodel;
}
queryterm = txtKeyword.Text;
}
if (cbplstMultipage.SelectedIndex > -1)
{
Session["multipage"] = string.Format("Multi-Page-{0},www.{1}.com", cbplstMultipage.SelectedItem.Text, txtMultiPage.Text.Trim());
strUri = strUri + string.Format("&site=www.{0}.{1}",txtMultiPage.Text.Trim(),cbplstMultipage.SelectedValue.Trim());
}
}
// if choose homepagefinding
if (cblstQueryType.SelectedValue.Trim().Equals("HP"))
{
Session["querytype"] = "Query Type" + "-" + cblstQueryType.SelectedItem.Text;
if (cbplstMultipage.SelectedIndex > -1)
{
queryterm = string.Format("hostname:www.{0}.{1}", txtKeyword.Text,cbplstMultipage.SelectedValue.Trim());
Session["multipage"] = "Multi-Page" + "-" + cbplstMultipage.SelectedItem.Text;
}
else
{
Response.Write("Please choose multiplage option");
return;
}
}
// if choose namepage finding
if (cblstQueryType.SelectedValue.Trim().Equals("NP"))
{
Session["querytype"] = "Query Type" + "-" + cblstQueryType.SelectedItem.Text;
queryterm = string.Format("url:http://{0}", txtKeyword.Text);
}
////if choose multipage
//if (cbplstMultipage.C)
//{
// strUri = strUri + string.Format("&site={0}", txtSite.Text);
// // queryterm = string.Format("link:{0}", txtKeyword.Text);
//}
//if choose page model "Anchor text"
//inurl: use to find a specific keyword as part of indexed urls.
//Example: inurl:bulgarian
if (cblstSPage.SelectedValue.Equals("Anchor Text"))
{
Session["singlepage"] = "Single Page" + "-" + cblstSPage.SelectedItem.Text;
queryterm = string.Format("inurl:{0}", txtKeyword.Text);
}
if (cblstSPage.SelectedValue.Equals("URL"))
{
Session["singlepage"] = "Single Page" + "-" + cblstSPage.SelectedItem.Text;
queryterm = string.Format("inurl:{0}", txtKeyword.Text);
}
//if choose page model "table","Font color","Font size","Font color","Frame","Meta
//if choose query model- link structure- Authority-Hub
if (cblstLinkStruc.SelectedValue.Equals("AH"))
{
Session["linkstructure"] = cblstLinkStruc.SelectedItem.Text;
if (cbplstMultipage.SelectedIndex > -1)
{
queryterm = string.Format("link:http://www.{0}.{1}+{2}", txtMultiPage.Text.Trim(), cbplstMultipage.SelectedValue.Trim(), txtKeyword.Text.Trim());
Session["multipage"] = string.Format("Multi-Page-{0},www.{1}.com" , cbplstMultipage.SelectedItem.Text,txtMultiPage.Text.Trim());
}
else
{
Response.Write("Please choose multiplage option");
return;
}
}
//if choose query model- link structure-PageRank
//if choose query model-Similarity-VSM
if (cblstSimilarity.SelectedValue.Equals("VSM"))
{
strUri = strUri + "&type=any";
}
//if choose query model-Similarity-Okapi
if (cblstSimilarity.SelectedValue.Equals("Okapi"))
{
strUri = strUri + "&type=any";
}
//if choose query model-Similarity-CDR
if (cblstSimilarity.SelectedValue.Equals("CDR"))
{
strUri = strUri + "&type=all";
}
//if choose query model-Similarity-TLS sub-phrases
if (cblstSimilarity.SelectedValue.Equals("TLS"))
{
Session["similarity"] = "Similarity"+"-"+cblstSimilarity.SelectedItem.Text;
strUri = strUri + "&type=phrase";
queryterm = txtKeyword.Text;
}
//if choose query model-Synonym
if (cbSynonym.Checked)
{
Session["synonym"] ="Synonym"+"-"+ cbSynonym.Text;
strUri = strUri + "&similar_ok=1";
queryterm = txtKeyword.Text;
}
if (!queryterm.Equals(""))
{
strUri = strUri + string.Format("&query={0}", queryterm);
}
Session["queryterm"] = txtKeyword.Text;
Session["requestUri"] = strUri;
Server.Transfer("QueryScript.aspx");
//Server.Transfer(string.Format("Result.aspx?query={0}",strUri));
// Create the web request
//HttpWebRequest request = WebRequest.Create(strUri) as HttpWebRequest;
//// string result = "";
//XmlReader reader;
//string result = "";
//dtBegin = DateTime.Now;
//Response.Write(dtBegin.ToLongTimeString());
//Response.Write("
");
//// Get response
//using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
//{
// // Load data into a dataset
// if (response != null)
// {
// DataSet dsWeather = new DataSet();
// dsWeather.ReadXml(response.GetResponseStream());
// // Print dataset information
// dtEnd = DateTime.Now;
// ts = dtBegin.Subtract(dtBegin);
// Response.Write(ts.TotalMilliseconds.ToString());
// PrintDataSet(dsWeather);
// }
// else
// {
// Response.Write("Not found any related result");
// }
//}
}
protected void Button1_Click1(object sender, EventArgs e)
{
}
}