[英]C# Multiple HTTP posting issue with invalid URL
I am having trouble making calls to multiple URI's , currently this works in its current form but if the site is unreachable (does not exist) it breaks the other calls where I need it to just ignore and continue . 我在拨打多个URI时遇到麻烦,目前这可以其当前形式运行,但是如果该站点无法访问(不存在),则会打断我需要忽略并继续的其他调用。 This was built of a single posting method , but now the requirements have changed.
这是通过单一发布方法构建的,但是现在需求已更改。
I have little experience with threading but this is what I've done to create the threads. 我对线程的经验很少,但这是我创建线程所做的。
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 grabs the URL and if it needs to use the proxy or not and passes it to the make thread method above which passes the xml , uri & proxy as an object to HTTP Post. SetUrls捕获URL,如果需要使用或不使用代理,则将其传递给make线程方法,该方法上面将xml,uri和proxy作为对象传递给HTTP Post。
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
If I have 2 working URL's it works . 如果我有2个有效的网址,则可以使用。 If I try 3 urls (where the 2nd is invalid) the first one will finish but the other will not, even after the 2nd thread exits due to timeout!.
如果我尝试3个url(其中第二个无效),即使第2个线程由于超时而退出,第一个也将完成,而另一个则不会。
I presume I am not using the threads correctly but I cannot see where any help would be appreciated . 我想我没有正确使用线程,但是看不到任何帮助。
Thank you. 谢谢。
您需要通过配置文件或通过以下属性来提高并发连接的数量: DefaultConnectionLimit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.