我有一个正在使用网站的WCF服务。在任何人指出这种方法中明显的缺陷和内在的不稳定性之前,请原谅我,我被迫为此工作。
交易流程如下:
频道 - > WCF服务 - >(HTTP POST / GET) - >网站
步骤: PostStrOnUrlRandom在一个循环中被调用,传递它要发布的字符串(strPost),url(url)和CookieJar(CookieJarR)包含来自上一篇文章的cookie。
我必须为5个网址执行此操作。然后我必须将回复发送回频道。然后频道向我发送另一个请求,我必须再做一次POST。我这样做的方法是使用我之前提出的相同的cookie。
这就是出现问题的地方,我收到内部服务器错误500.如果我在这个POST上退休,使用相同的strPost,url和Cookies,它将在3-4退休后工作。我不明白为什么会这样。
代码如下:
public HttpWebPostResponse PostStrOnUrlRandom(string url, string strPost, string Refer, CookieCollection cookieJarR)
{
log.Debug("Method Entery [PostStrOnUrl]");
StreamWriter myWriter = null;
string resultHtml = null;
HttpWebResponse objResponse = null;
HttpWebPostResponse hpr = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.ProtocolVersion = HttpVersion.Version10;
objRequest.Timeout = 90000;
objRequest.CookieContainer = cookieJar;
objRequest.Method = "POST";
objRequest.ContentType = "application/x-www-form-urlencoded";
objRequest.AllowAutoRedirect = true;
objRequest.MaximumAutomaticRedirections = 100;
objRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0";
objRequest.Referer = Refer;
objRequest.Host = ConfigurationManager.AppSettings["Host"];
objRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
objRequest.Headers.Add("Origin", ConfigurationManager.AppSettings["Origin"]);
objRequest.Headers.Add("Cache-Control", "max-age=0");
objRequest.Headers.Add("Accept-Encoding", "gzip, deflate, br");
objRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
objRequest.KeepAlive = true;
log.Debug("Making HttpWebRequest to" + url);
Uri target = new Uri(url);
log.Debug("Target Url : " + url.ToString());
foreach (Cookie cookie in cookieJarR)
{
objRequest.CookieContainer.Add(cookie);
cookieJar.Add(cookie);
log.Debug(cookie.Name + "" + cookie.Value + "" + "" + cookie.Domain);
}
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
log.Debug("Posting on Url StrPost =" + strPost);
}
catch (Exception e)
{
log.Debug(e.Message);
}
finally
{
myWriter.Close();
}
log.Debug("Making Request.");
try
{
objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
resultHtml = sr.ReadToEnd();
sr.Close();
}
hpr = new HttpWebPostResponse(objResponse.ResponseUri.ToString(), resultHtml, objResponse.StatusCode.ToString());
return hpr;
}
catch (Exception e)
{
log.Error(e.Message);
log.Error("Exception", e.InnerException);
return null;
}
}