C#Web服务 - 在IE中运行,但不在Safari中运行

时间:2009-11-10 21:58:54

标签: c# .net web-services safari internet-explorer

我有一个简单的网络服务,我想通过http帖子访问。 网络服务代码如下所示:

[WebMethod]
public int Insert(string userDate, string DeviceID)
{

    bool output;
    DateTime date;
    output = DateTime.TryParse(userDate, out date);
    if (!output)
    {
        // Throw an error
        return -1;
    }
    int Device;
    output = int.TryParse(DeviceID, out Device);
    if (!output)
    {
        // Throw an Error
        return -1;
    }
    UsersDatesBLL BLL = new UsersDatesBLL();
    return BLL.Insert(Device, date);
}

我可以使用Internet Explorer正常访问服务,只需调用以下内容即可将结果完美地插入到数据库中:CountDownService.asmx / Insert?userDate = 24/04/208& DeviceID = 3435 但是,在Safari和Firefox上进行测试时,服务始终返回-1

有谁知道这个的原因? Safari编码字符串的方式与IE不同吗?

此致 米克

3 个答案:

答案 0 :(得分:1)

用户可以在浏览器中配置他们的UI语言和文化。浏览器将此信息作为Accept-Language HTTP header传递给您的Web服务请求。处理请求的That information may be used to set the "current culture" of the ASP.NET session。它可用作静态属性CultureInfo.CurrentCulture

DateTime.TryParse将使用该“当前文化”来确定它应该期望的许多日期时间字符串格式中的哪一种 - 除非您使用显式传递文化的overload作为{{1} }。显然,您正在测试的浏览器配置不同,因此ASP.NET需要不同的日期时间格式。如果您希望独立于浏览器设置解析日期时间,则应使用CultureInfo.InvariantCulture作为格式提供者。

答案 1 :(得分:0)

我作为调试措施做的第一件事(假设您不能只在服务器代码上运行调试器)将使所有三个返回路径返回不同的值。这样您就不会猜测是DateTime.TryParse()来电,int.TryParse()来电还是失败的BLL.Insert()来电。

如果BLL.Insert()失败时返回-1,那么我会将第一个-1更改为-3而第二个更改为-2。像这样:

output = DateTime.TryParse(userDate, out date);
if (!output)
{
    // Throw an error
    return -3;  // ***** Changed this line
}
int Device;
output = int.TryParse(DeviceID, out Device);
if (!output)
{
    // Throw an Error
    return -2;  // ***** Changed this line
}

我知道它并没有完全回答这个问题,但它至少可以帮助你追踪哪个部分失败。

答案 2 :(得分:0)

您正在使用当前文化来解析DateTimeint值。首先要检查的是,您的各种浏览器是否都配置为在其HTTP请求标头中将相同的文化发送到Web服务器。

作为一种风格问题,您应该避免使用与文化相关的URL参数格式。最合适的格式是固定的XML Schema格式,如下所示:

[WebMethod]
public int Insert(string userDate, string DeviceID) {

    DateTime date;
    int device;
    try {
        date = XmlConvert.ToDateTime(userDate, XmlDateTimeSerializationMode.Local);
        device = XmlConvert.ToInt32(DeviceID);
    } catch (Exception) {
        // Throw an error
        return -1;
    }

    UsersDatesBLL BLL = new UsersDatesBLL();
    return BLL.Insert(device, date);
}

并称之为:

CountDownService.asmx/Insert?userDate=1980-04-24&DeviceID=3435

另一种方法是使用强类型参数值,让ASP.NET为您进行解析,例如。

[WebMethod]
public int Insert(DateTime userDate, int DeviceID) {

    UsersDatesBLL BLL = new UsersDatesBLL();
    return BLL.Insert(DeviceID, userDate);
}

免责声明 - 我自己没有尝试过这种替代方案,但值得一看,因为如果您不必将解析代码放入每个Web方法中,它将使您的生活更轻松。