我正在尝试将某些JSON
数据作为cron作业(定期)获取并将其存储到数据库。所以,我在Azure
雇用了一个工人角色。但是http requests
失败了。这是代码:
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.TraceInformation("WorkerRole1 entry point called", "Information");
while (count <= 10)
{
WebClient wc = new WebClient();
string download = "http://api.openweathermap.org/data/2.5/weather?q=Kolkata&units=metric";
string json = wc.DownloadString(download);
var jObj = JsonConvert.DeserializeObject(json) as JObject;
List<JObject> result = jObj["weather"].Children()
.Cast<JObject>()
.ToList();
var final = result[0].Children()
.Cast<JProperty>()
.Select(x => new
{
temp = (string)x.Value,
}).ToList();
var temp = jObj["main"]["temp"];
var humidity = jObj["main"]["humidity"];
var condition = final[2].temp;
/*SQL insert query for weather data*/
con.Open();
cmd = new SqlCommand("INSERT INTO WEATHER(id,temperature,humidity,condition) VALUES(@id,@temp,@humid,@cond)", con);
cmd.Parameters.AddWithValue("@id", count);
cmd.Parameters.AddWithValue("@temp", Convert.ToInt32(temp));
cmd.Parameters.AddWithValue("@humid", Convert.ToInt32(humidity));
cmd.Parameters.AddWithValue("@cond", condition.ToString());
cmd.ExecuteNonQuery();
con.Close();
Trace.TraceInformation("Working", "Information");
count++;
}
}
请告诉我是什么问题,并为我提供解决方案......