2012-05-21 14 views
0

我無法對多個URI進行調用,目前這在當前形式工作,但如果該網站無法訪問(不存在)它打破了我需要它的其他調用只是忽略並繼續。這是由單一發布方法構建的,但現在需求發生了變化。C#多個HTTP發佈問題與無效的URL

我幾乎沒有線程的使用經驗,但這是我爲創建線程所做的。

private void makethread(string Url, string useproxy, string xml) 
{ 
    //Parameters given put into list and passed with Thread. 
    var urls = new List<string>() { Url, useproxy, xml }; 

    //Create thread 

    Thread t1 = new Thread(new ParameterizedThreadStart(HttpPost)); 
    t1.Start(urls); 


} 

    public void SetUrls() 
{ 
    String Url; 
    int count = 1; 

    string xml = CreateXML(); 
    string useproxy = "(none)"; 
    do 
    { 
     Url = objIniFile.GetString("Post", count.ToString(), "(none)"); 
     useproxy = objIniFile.GetString("Post", count.ToString() + "Proxy", "(none)").ToUpper() ; 
     if (Url != "(none)") 
     { 

      // HttpPost(Url, xml , useproxy); 
      makethread(Url , useproxy , xml); 


     } 
     count++; 
    } 
    while (Url != "(none)"); 
} 

SetUrls抓起URL和如果需要使用的代理或不併將其傳遞到高於該通XML,URI &代理作爲一個目的是HTTP投遞廠名螺紋方法。

public void HttpPost(object urls) 
{ 

    string url= "none"; 
    string xml= "none"; 
    string useproxy = "none"; 

    int count = 0; 
    //Splits Object to list and populate variables 
    foreach (var item in urls as List<string>) 
    { 
     if (count == 0) { url = item; } 
     if (count == 1) { useproxy = item; } 
     if (count == 2) { xml = item; } 
     count++; 
    } 

    string vystup = null; 
    string proxy = ProxyAddress; 

    string postData = xml; 
    //-------------- 

    try 
    { 
     HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); 
     HttpWebRequest.DefaultCachePolicy = policy; 


     byte[] buffer = Encoding.UTF8.GetBytes(postData); 
     //Initialisation 
     HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url); 
     //method is post 

     if (useproxy == "ON") 
     { 
      WebProxy myProxy = new WebProxy(); 
      // Create a new Uri object. 
      Uri newUri = new Uri(proxy); 

      // Associate the new Uri object to the myProxy object. 
      myProxy.Address = newUri; 
      WebReq.Proxy = myProxy; 
     } 

     WebReq.KeepAlive = false; 
     WebReq.Method = "POST"; 
     WebReq.ContentType = "application/x-www-form-urlencoded"; 
     //The length of the buffer 
     WebReq.ContentLength = buffer.Length; 
     Stream PostData = WebReq.GetRequestStream(); 
     //write, and close. 
     PostData.Write(buffer, 0, buffer.Length); 
     PostData.Close(); 

     //Get the response handle 
     HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); 
     //Let's show some information about the response 
     Console.WriteLine(WebResp.StatusCode); 
     Console.WriteLine(WebResp.Server); 

     //Do not worry about response!. 
     //read the response (the string), and output it. 
     Stream Answer = WebResp.GetResponseStream(); 
     StreamReader _Answer = new StreamReader(Answer); 
     vystup = _Answer.ReadToEnd(); 


     // MessageLog("Finished Posting " + DateTime.Now, "INFO"); 

     if (vystup != "OK") 
     { 

     } 
    } 
    catch (WebException ex) 
    { 

     Console.WriteLine(ex); 
    } 



} 
#endregion 

如果我有2個工作網址它的作品。如果我嘗試3個URL(第二個無效),第一個將完成,但另一個不會,即使第二個線程由於超時而退出!

我認爲我沒有正確使用線程,但我看不到任何幫助,將不勝感激。

謝謝。

回答