Google news API using ASP.NET C#

Google provides huge range of API for searching,maps,news and much more.. This article is related to Google news API , Just enter keyword for news and you will get results for keyword. I have used Ajax post method for Google news API.


This article is reagarding google news API.
Google provides API for news , We have to just request and it will provide result in json and xml format.
I have used AJAX POST methos for request to Google API.

We have to provide keyword to get data from Google API.
All the procedure and Code snippet i have written below.


I have used HttpWebRequest and HttpWebResponse for retrieving news from Google API URL.
I have one textbox for search parameters in using Google API.
When we click enter on textbox after type keyword in textbox, it calls JavaScript GetNews() function.
From JavaScript function, Ajax post method calls web method from code behind.
Google News content has been retrieved using HttpWebRequest and HttpWebRequest, this WebMethod returns data into array format from code behind.
Now Div having name "DivNews" has been appended for showing news content using jQuery.
For further details, please take a look at the code.

Code Snippet

I have written code for Javascript,css,HTML and C# below.

1) Javascript Code

 //For Enter Key Press Event
function runScript(e) {
if (e.keyCode == 13) {

//Call GetNews Function

return false;

//Function for GetNews Using Ajax Post Method
function GetNews() {

//Set FadeIn for Progressive Div

//Create Ajax Post Method
type: "POST", // Ajax Method
url: "News.aspx/GetNewsContent", //Page URL / Method name
data: "{'NewsParameters':'" +
document.getElementById("txtSubject").value + "'}", // Pass Parameters
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) { // Function call on success

$("#DivNews").empty(); // Set Div Empty

//Set For loop for binding Div Row wise
for (var i = 0; i < data.d.length; i++) {

//Design Div Dynamically using append
<B style='color:Red'>" + data.d[i].title +
"- By: Nirav Prabtani</B> </td></tr><tr><td>" +
data.d[i].Description + "</td></tr>");

//set fadeOut for ProgressiveDiv

error: function (result) { // Function call on failure or error

2) CSS

-moz-box-shadow: inset 0px 1px 0px 0px #ffffff;
-webkit-box-shadow: inset 0px 1px 0px 0px #ffffff;
box-shadow: inset 0px 1px 0px 0px #ffffff;
background: -webkit-gradient( linear, left top,
left bottom, color-stop(0.05, #ededed), color-stop(1, #dfdfdf) );
background: -moz-linear-gradient( center top, #ededed 5%, #dfdfdf 100% );
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#dfdfdf');
background-color: #ededed;
-webkit-border-top-left-radius: 6px;
-moz-border-radius-topleft: 6px;
border-top-left-radius: 6px;
-webkit-border-top-right-radius: 6px;
-moz-border-radius-topright: 6px;
border-top-right-radius: 6px;
-webkit-border-bottom-right-radius: 6px;
-moz-border-radius-bottomright: 6px;
border-bottom-right-radius: 6px;
-webkit-border-bottom-left-radius: 6px;
-moz-border-radius-bottomleft: 6px;
border-bottom-left-radius: 6px;
text-indent: 0;
border: 1px solid #dcdcdc;
display: inline-block;
color: #777777;
font-family: arial;
font-size: 15px;
font-weight: bold;
font-style: normal;
height: 25px;
line-height: 50px;
width: 100px;
text-decoration: none;
text-align: center;
text-shadow: 1px 1px 0px #ffffff;
background: -webkit-gradient( linear, left top,
left bottom, color-stop(0.05, #dfdfdf), color-stop(1, #ededed) );
background: -moz-linear-gradient( center top, #dfdfdf 5%, #ededed 100% );
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#dfdfdf', endColorstr='#ededed');
background-color: #dfdfdf;
position: relative;
top: 1px;

background: #FFF url( no-repeat 4px 4px;
border: 1px solid #999;
outline: 0;
padding-left: 25px;
height: 25px;
width: 275px;
height: 61px;
width: 100%;
height: 100%;
display: none;
opacity: 0.4;
position: fixed;
top: 0px;
left: 0px;
vertical-align: middle;


<td align="center" class="style1">
Welcome to My news Portal</h3>
<asp:TextBox runat="server" ID="txtSubject"
CssClass="textbox" onkeypress="return runScript(event)" />
<td align="right">
<h6 style="height: 35px">
By: Nirav Prabtani</h6>
<div id="DivNews">

<%--This Div is For Binding News--%>
<div id="ProgressiveDiv" style="padding-left: 500px">
<img src="Image/loading.gif" />

4) C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Net;
using System.IO;
using System.Text;
using System.Data;

namespace GoogleNews_API
public partial class News : System.Web.UI.Page
//Page Load Method
protected void Page_Load(object sender, EventArgs e)


//WenMethod GetNewsContent for Retrieving News from Google API
public static ItemNews[] GetNewsContent(string NewsParameters)

List<ItemNews> Details = new List<ItemNews>();

// httpWebRequest with API URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create
("" + NewsParameters + "&output=rss");

//Method GET
request.Method = "GET";

//HttpWebResponse for result
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//Mapping of status code
if (response.StatusCode == HttpStatusCode.OK)
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;

if (response.CharacterSet == "")
readStream = new StreamReader(receiveStream);
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));

//Get news data in json string

string data = readStream.ReadToEnd();

//Declare DataSet for putting data in it.
DataSet ds = new DataSet();
StringReader reader = new StringReader(data);
DataTable dtGetNews = new DataTable();

if (ds.Tables.Count > 3)
dtGetNews = ds.Tables["item"];

foreach (DataRow dtRow in dtGetNews.Rows)
ItemNews DataObj = new ItemNews();
DataObj.title = dtRow["title"].ToString(); = dtRow["link"].ToString();
DataObj.item_id = dtRow["item_id"].ToString();
DataObj.PubDate = dtRow["pubDate"].ToString();
DataObj.Description = dtRow["description"].ToString();

//Return News array
return Details.ToArray();

//Define Class to return news data
public class ItemNews
public string title { get; set; }
public string link { get; set; }
public string item_id { get; set; }
public string PubDate { get; set; }
public string Description { get; set; }


Google news API


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: