Skip to content

Commit

Permalink
1) Change channel info URL. 2) Add gzip compression support for HTTP …
Browse files Browse the repository at this point in the history
…request.
  • Loading branch information
kfstorm authored and Kai Yang committed Jun 12, 2015
1 parent 20768ed commit 611d7f6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
56 changes: 44 additions & 12 deletions DoubanFM.Core/ConnectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Net;
using System.IO;
Expand Down Expand Up @@ -50,11 +52,15 @@ public class ConnectionBase
/// 编码
/// </summary>
public Encoding Encoding;
/// <summary>
/// 是否抛出异常
/// </summary>
public bool ThrowException;
/// <summary>
/// <summary>
/// 是否抛出异常
/// </summary>
public bool ThrowException;
/// <summary>
/// 是否使用Gzip编码
/// </summary>
public bool UseGzip = true;
/// <summary>
/// 数据保存文件夹
/// </summary>
public static readonly string DataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"K.F.Storm\豆瓣电台");
Expand Down Expand Up @@ -110,11 +116,24 @@ public string Post(string postUri, string accept, string referer, string content
request.Referer = referer;
request.UserAgent = UserAgent;
request.ServicePoint.Expect100Continue = false;
using (Stream requestStream = request.GetRequestStream())
if (UseGzip)
{
request.Headers["Accept-Encoding"] = "gzip, deflate";
}
using (var requestStream = request.GetRequestStream())
requestStream.Write(content, 0, content.Length);
using (HttpWebResponse responce = request.GetResponse() as HttpWebResponse)
using (StreamReader sr = new StreamReader(responce.GetResponseStream(), Encoding))
file = sr.ReadToEnd();
using (var response = request.GetResponse() as HttpWebResponse)
{
var stream = response.GetResponseStream();
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
stream = new GZipStream(stream, CompressionMode.Decompress);
}
using (var sr = new StreamReader(stream, Encoding))
{
file = sr.ReadToEnd();
}
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -169,9 +188,22 @@ public string Get(string getUri, string accept, string referer)
request.Method = "GET";
request.Referer = referer;
request.UserAgent = UserAgent;
using (HttpWebResponse responce = request.GetResponse() as HttpWebResponse)
using (StreamReader sr = new StreamReader(responce.GetResponseStream(), Encoding))
file = sr.ReadToEnd();
if (UseGzip)
{
request.Headers["Accept-Encoding"] = "gzip, deflate";
}
using (var response = request.GetResponse() as HttpWebResponse)
{
var stream = response.GetResponseStream();
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
stream = new GZipStream(stream, CompressionMode.Decompress);
}
using (var sr = new StreamReader(stream, Encoding))
{
file = sr.ReadToEnd();
}
}
}
catch (Exception ex)
{
Expand Down
5 changes: 4 additions & 1 deletion DoubanFM.Core/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.IO;
using System.Threading;
Expand Down Expand Up @@ -490,7 +491,9 @@ private static ChannelInfo GetChannelInfo()
++tryCount;

Debug.WriteLine(DateTime.Now + " 获取频道列表……");
var file = new ConnectionBase().Get("http://doubanfmcloud-channelinfo.stor.sinaapp.com/channelinfo");
var file = new ConnectionBase {UseGzip = true}.Get(
string.Format(ConfigurationManager.AppSettings["Player.ChannelInfoUrlFormat"],
typeof (Player).Assembly.GetName().Version));
Debug.WriteLine(DateTime.Now + " 获取频道列表完成");
if (string.IsNullOrEmpty(file))
{
Expand Down
2 changes: 1 addition & 1 deletion DoubanFM.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
//
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
[assembly: AssemblyVersion("2.1.2.2")]
[assembly: AssemblyVersion("2.1.3.0")]
//[assembly: AssemblyVersion("1.0.0.0")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
1 change: 1 addition & 0 deletions DoubanFM/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<!-- DoubanFM.Core -->
<add key="Player.ChannelInfoExpireSeconds" value="432000"/>
<add key="Player.ChannelInfoRetryCount" value="3"/>
<add key="Player.ChannelInfoUrlFormat" value="http://doubanfm.azurewebsites.net/GetChannelInfo.aspx?version={0}"/>


<!-- DoubanFM.Bass -->
Expand Down
2 changes: 1 addition & 1 deletion version.dat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.2.2
2.1.3.0

0 comments on commit 611d7f6

Please sign in to comment.