level 1
_聂伟
楼主
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string url = "http://apis.baidu.com/apistore/weatherservice/cityid";
string param = "cityid=101010100";//GET的参数
//返回json
string callback = GETMoths(url,param);
//显示
Console.WriteLine(callback);
}
//GET方式发送得结果
public static string GETMoths(string url, string param)
{
string strURL = url;
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "GET";
// 添加 header 参数
request.Headers.Add("apikey", "c59c672cb5841b143a3b9abf43795a73");//apikey链接key
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = param;
byte[] payload;
payload =Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();//此处有异常
writer.Write(payload, 0, payload.Length);
writer.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}
}
}
2015年05月29日 13点05分
1
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string url = "http://apis.baidu.com/apistore/weatherservice/cityid";
string param = "cityid=101010100";//GET的参数
//返回json
string callback = GETMoths(url,param);
//显示
Console.WriteLine(callback);
}
//GET方式发送得结果
public static string GETMoths(string url, string param)
{
string strURL = url;
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "GET";
// 添加 header 参数
request.Headers.Add("apikey", "c59c672cb5841b143a3b9abf43795a73");//apikey链接key
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = param;
byte[] payload;
payload =Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();//此处有异常
writer.Write(payload, 0, payload.Length);
writer.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}
}
}
