【问题标题】:Can't post file data when request is sent over SSL通过 SSL 发送请求时无法发布文件数据
【发布时间】:2011-02-04 12:37:49
【问题描述】:

我在尝试使用自定义基本身份验证模块 similar to this 时遇到问题。客户端使用HttpWebRequest 类。

客户端运行如下代码:

void uploadFile( string serverUrl, string filePath )
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.
        Create( serverUrl );
    CredentialCache cache = new CredentialCache();
    cache.Add( new Uri( serverUrl ), "Basic", new NetworkCredential( "User", "pass" ) );
    request.Credentials = cache;
    request.Method = "POST";
    request.ContentType = "application/octet-stream";
    request.Timeout = 60000;
    request.KeepAlive = true;

    using( BinaryReader reader = new BinaryReader( 
        File.OpenRead( filePath ) ) ) {

        request.ContentLength = reader.BaseStream.Length;
        using( Stream stream = request.GetRequestStream() ) {
            byte[] buffer = new byte[1024];
            while( true ) {
                int bytesRead = reader.Read( buffer, 0, buffer.Length );
                if( bytesRead == 0 ) {
                    break;
                }
                stream.Write( buffer, 0, bytesRead );
            }
        }
    }

     HttpWebResponse result = (HttpWebResponse)request.GetResponse();
     //handle result - not relevant
 }

如果请求是为以http:// 开头的URI 创建的,它可以正常工作 - 请求到达服务器,身份验证模块通过请求,它以WWW-Authenticate 回复,现在使用身份验证参数重复请求,模块对其进行验证并进一步通过。

如果请求是为以https:// 开头的 URI 创建的,则它不起作用。初始请求到达模块,模块回复WWW-Authenticate

void ReplyWithAuthHeader()
{
    HttpContext currentContext = HttpContext.Current;
    context.Response.StatusCode = 401;
    context.Response.AddHeader( "WWW-Authenticate",
       String.Format("Basic realm=\"{0}\"", "myname.mycompany.com"));
}

在客户端引发异常,并显示“无法将数据写入传输连接:已建立的连接已被主机中的软件中止。”文本。

我尝试了System.Net tracing,发现在发送初始请求后,客户端会返回以下标头:

Date: Fri, 04 Feb 2011 12:15:04 GMT
Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET

当 URI 以 http:// 开头时,客户端收到以下信息:

Content-Length: 1894
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Fri, 04 Feb 2011 12:12:11 GMT
Server: Microsoft-IIS/5.1
WWW-Authenticate: Basic realm="myname.mycompany.com"
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET

很明显,WWW-Authenticate 响应在某处被吞没了,并没有到达客户端。

此外,如果我排除将文件数据写入请求的代码,它也可以进行身份​​验证。

我该如何解决这个问题?如何使WWW-Authenticate 响应到达客户端?

【问题讨论】:

    标签: c# .net asp.net iis ssl


    【解决方案1】:

    服务器返回的http状态码是什么? 服务器的证书是否有效? 你能发布堆栈跟踪吗?

    尝试在客户端上禁用防火墙和其他网络工具,如提琴手等。 也可以尝试将 KeepAlive 属性设置为 false。

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      相关资源
      最近更新 更多