hi all
i have a little problem regarding string manupulation.
actaully i have a variable named "result"
actually i have a textbox and button ,i can paste the url in textbox and click on button then web request sent and get the web response in the variable "result"
after when i get response in variable i want to take some data from that
ex:
http://www.flipkart.com/essential-english-grammar-self-study-reference-practice-book-elementary-students-answers-2nd/p/itmdyuchmsxjs2h3?pid=9788175960299&srno=b_2&ref=e23b0958-64df-43cf-9dc8-aff5ec3d1919[
^]
i have used this url to get book name,book imagepath,bookisbnno10,bookisbnno13 from this and insert in database .
but i have no idea how can i achieve this,
the code i have written is :
protected void Button1_Click(object sender, EventArgs e)
{
WebRequest request = WebRequest.Create(TextBox1.Text);
request.Method = "POST";
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
int firstIndex = responseFromServer.IndexOf("<body");
string result = responseFromServer.Substring(firstIndex + 6, responseFromServer.LastIndexOf("</body>") - (firstIndex + 6));
Regex rRemScript = new Regex(@"<script [^>]*>[\s\S]*?</script>");
result = rRemScript.Replace(result, "");
Regex rstyleScript = new Regex(@"<style [^>]*>[\s\S]*?</style>");
result = rstyleScript.Replace(result, "");
result = Regex.Replace(result, @"\s+", "");
getdata(result);
}
public void getdata(string result)
{
string booktitle = book_title(result);
string imagepath = image_path(result);
string originalprice = original_price(result);
string markdownprice = markdown_price(result);
string ISBN10 = ISBN_10(result);
string ISBN13 = ISBN_13(result);
insertdata(booktitle, imagepath, originalprice, markdownprice, ISBN10, ISBN13);
}
public string book_title(string result)
{
string strRegex = @"<h1 sitemprop="" name="">(.*?)</h1>";
Match output = Regex.Match(result, strRegex, RegexOptions.Singleline | RegexOptions.IgnoreCase);
return output.ToString();
}
public string image_path(string result)
{
string strRegex = @"src=""(.*?)""";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase);
string url = myRegex.Match(s).Groups[1].Value;
return url;
}
public string original_price(string result)
{
Regex original_price = new Regex(@"<span class="price list old-price" id="fk-mprod-list-id">Rs.[^>]*>[\s\S]*?</span>");
string imagepath = original_price.ToString();
return imagepath;
}
public string markdown_price(string result)
{
Regex markdown_price = new Regex(@"<span class="fk-font-26 pprice fk-bold"> Rs.[^>]*>[\s\S]*?</span>");
string markdownprice = markdown_price.ToString();
return markdownprice;
}
public string ISBN_10(string result)
{
Regex ISBN_10 = new Regex(@" <td class="specs-key">ISBN-10</td><td class="specs-value fk-data">[^>]*>[\s\S]*?</td>");
string ISBN10 = ISBN_10.ToString();
return ISBN10;
}
public string ISBN_13(string result)
{
Regex ISBN_13 = new Regex(@"<td class="specs-key">ISBN-13</td><td class="specs-value fk-data">[^>]*>[\s\S]*?</td>");
string ISBN13 = ISBN_13.ToString();
return ISBN13;
}
public void insertdata(string book_name, string imagepath, string original_price, string our_price, string ISBN10, string ISBN13)
{
connection = new SqlConnection(strcon);
command = new SqlCommand("usp_Ins_book_details", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@book_name", book_name);
command.Parameters.AddWithValue("@imagepath", imagepath);
command.Parameters.AddWithValue("@original_price", original_price);
command.Parameters.AddWithValue("@our_price", our_price);
command.Parameters.AddWithValue("@ISBN10", ISBN10);
command.Parameters.AddWithValue("@ISBN13", ISBN13);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
i have spent more time in that but not getting answer
please hlp me
thanks in advance..