2
託管一個web服務,它可以爲ASPX頁面做一些POST
。從網絡服務POST到服務器
代碼:
[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;
}
}
任何人都可以解釋運行第一個代碼的問題嗎?他們阻止了什麼嗎?