【问题标题】:Doing POST to server from web-service从 Web 服务向服务器发送 POST
【发布时间】:2013-07-31 09:36:19
【问题描述】:

托管了一个对 ASPX 页面执行POST 的 Web 服务。

代码:

 [WebMethod]
        public string Test()
        {
            sb.AppendLine("Start");
            try
            {
                var t = new Thread(MyThreadStartMethod);
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
                t.Join();
            }
            catch (Exception ex)
            {
                sb = sb.AppendLine(ex.ToString());
            }
            sb.AppendLine("Finish");
            return sb.ToString();
        }

         private void MyThreadStartMethod(object obj)
        {
            try
            {
                WebBrowser browser = new WebBrowser();
                browser.DocumentCompleted += browser_DocumentCompleted;
                browser.Url = new Uri("http://www.wikipedia.com");
                while (browser.ReadyState != WebBrowserReadyState.Complete)
                {
                    System.Windows.Forms.Application.DoEvents();
                    Thread.Sleep(60);
                }
            }
            catch (Exception ex)
            {
                sb = sb.AppendLine(ex.ToString());
            }
        }

        void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            sb = sb.AppendLine("Document completed: " + e.Url);
        }

当尝试在我的共享主机服务器上运行它时,我总是遇到超时错误。

尝试了下一个代码来检查我是否可以连接到第三方地址并且它工作正常。

private bool checkUrl(string url)
{
    try
    {
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "HEAD";
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        return (response.StatusCode == HttpStatusCode.OK);
    }
    catch
    {
        return false;
    }
}

谁能解释运行第一个代码的问题?他们会阻止什么吗?

【问题讨论】:

    标签: c# .net web-services browser


    【解决方案1】:

    您必须从代码中删除接下来的 2 行

    System.Windows.Forms.Application.DoEvents();
    Thread.Sleep(60);
    

    因为它正在阻止您的网络浏览器控件完成导航。

    Hans Passant 提出了关于如何在单独的线程中运行 WebBrowser 控件的best sample

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多