using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
content = reader.ReadToEnd();
}
}
}
}
6、计算一年中的周。
// 求某年有多少周
public static int GetYearWeekCount(int year)
{
int count = 53;
if (DateTime.IsLeapYear(year) && (new DateTime(year, 1, 1).DayOfWeek == DayOfWeek.Saturday))
{
count = 54;
}
return count;
}
// 求当前日期是一年的中第几周
public static int WeekOfYear(DateTime day)
{
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(day, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
}
7、如何在在WCF中使用HttpContext
默认情况下,通过WCF调用无法像ASMX或者ASPX一样可以直接使用HttpContext对象,当然也不可以使用其中的上下文缓存了。为了和现在系统蒹容可以使用WCF ASP.NET 兼容模式选项。在这种模式下,WCF 服务以与 ASMX 服务类似的方式参与 ASP.NET HTTP 管线。ASP.NET 功能(例如,文件授权、URL 授权和 HTTP 会话状态)适用于在此模式下运行的 WCF 服务。步聚如下:
A、程序员必须将 AspNetCompatibilityRequirementsAttribute 属性添加到服务类型中,并指定是允许还是需要 ASP.NET 兼容模式。
[ServiceBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class SampleContent : ISampleContentB、必须将应用程序配置为使用 ASP.NET 兼容模式。
此选项可以使您在将服务修改为使用 WCF 时,免于修改配置为使用 .asmx 服务文件 URL 的客户端。
Providing custom context to your WCF service instance
从开发的角度比较 ASP.NET Web 服务与 WCF
AspNetCompatibilityRequirementsAttribute
8、如何编写并发测试代码?
9、串联字符串
String.Join(",", new string[]{"a","b","c"})