المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : I need to move from google search result soap to search result html parser, any help?



C# Programming
10-06-2009, 05:10 AM
I need to move from google search result soap to search result html parser, any help?




private static string CreateMessage(string key, string query, int startIndex, int maxResults, bool safeSearch, bool filter, string country, string ********)
{

System.Text.StringBuilder message = new System.Text.StringBuilder();
message.Append("");
message.Append("");
message.Append(" ");
message.Append(" ");
message.Append(" " + key + "");
message.Append(" " + query + "");
message.Append(" " + (startIndex) + "");
message.Append(" " + maxResults + "");
message.Append(" " +filter.ToString().ToLower()+ "");
message.Append(" ");
message.Append(" " + safeSearch.ToString().ToLower() + "");
message.Append(" lang_" + ******** + ".country" + country.ToString().ToUpper() + "");
message.Append(" latin1");
message.Append(" latin1");
message.Append(" ");
message.Append(" ");
message.Append("");
return message.ToString();
}


private static GoogleWebSearchResponse DoSearchHelper(string key, string query, int startIndex, int maxResults, bool safeSearch, bool filter, string country, string ********)
{
string message = CreateMessage(key, query, startIndex, maxResults, safeSearch, filter, country, ********);
WebRequest soapRequest = WebRequest.Create("http://api.google.com/search/beta2");
//WebRequest soapRequest = WebRequest.Create("http://localhost:3833");
soapRequest.Headers.Add("SOAPAction", "urn:GoogleSearchAction");
soapRequest.Method = "POST";
soapRequest.ContentType = "text/xml; charset=utf-8";

using (Stream requestStream = soapRequest.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(message);
}
}

try
{
WebResponse soapResponse = soapRequest.GetResponse();
string responseText = null;
using (Stream responseStream = soapResponse.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
responseText = reader.ReadToEnd();
}
}

XmlDocument responseDoc = new XmlDocument();
responseDoc.LoadXml(responseText);
GoogleWebSearchResponse response = new GoogleWebSearchResponse(responseDoc);

return response;
}
catch (WebException ex)
{
using (Stream responseStream = ex.Response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
string responseText = reader.ReadToEnd();
}
}

//throw ex;
return null;
}
catch (Exception ex)
{
throw ex;
//return null;
}