Web服务中的httpwebrequest.getResponse超时

时间:2012-03-02 18:22:12

标签: c# .net web-services httpwebrequest httpwebresponse

Q1: 知道为什么下面的webservice永远不会从req.getResponse()返回并给出超时错误。

webservice代表用户创建httpwebrequest对象,并等待回调线程提供证书,然后返回结果。

我可能在这里遗失的任何东西?(可能是测试服务器上的一些延迟等)。 (请注意,我在外部webservice之前使用过httpwebrequest +回调没有任何问题)

Q2:是否存在从httpsserver检索证书的同步方式,而不是进行回调?

[WebService(Namespace = "http://testserver")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class main : System.Web.Services.WebService
{

     static main pointer;

    private static bool ValidateRemoteCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors policyErrors
)
    {
        X509Certificate2 cert = new X509Certificate2(certificate);
        HttpWebRequest wbr= (HttpWebRequest)sender;

        pointer.Application[wbr.RequestUri.ToString()] = cert.Thumbprint;
        return true;
    }



    public main()
    {
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateRemoteCertificate);            
        pointer = this;
    }
    [WebMethod]
    public string verifyCertificate(string thumb, string sslsite)
    {

        string result = "";


        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sslsite);
            pointer.Application[req.RequestUri.ToString()] = null;

            req.GetResponse();
           /* while (Application[req.RequestUri.ToString()] == null)
            {
                Thread.CurrentThread.Join(1000);
            }
            */
            result = (string)Application[req.RequestUri.ToString()];
        }
        catch (Exception e)
        {
            result = e.ToString();
        }
        return result;
    }
}

1 个答案:

答案 0 :(得分:0)

事实证明我在请求对象中缺少代理设置。一旦我启用它,它就可以工作