WebService に対して HttpWebRequest を実行しているときに、次のエラーが発生することがあります。以下のコードもコピーしました。
System.Net.WebException: リモート サーバーに接続できません ---> System.Net.Sockets.SocketException: ターゲット マシンがアクティブに拒否したため、接続できませんでした 127.0.0.1:80 System.Net.Sockets.Socket.DoConnect (エンドポイント endPointSnapshot、SocketAddress socketAddress) で System.Net.Sockets.Socket.InternalConnect (エンドポイント リモート EP) で System.Net.ServicePoint.ConnectSocketInternal で (ブール値の connectFailure、ソケット s4、ソケット s6、Socket& ソケット、IPAddress& アドレス、ConnectSocketState 状態、IAsyncResult asyncResult、Int32 タイムアウト、Exception& 例外) --- 内部例外スタック トレースの終了 --- System.Net.HttpWebRequest.GetRequestStream() で
ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.PreAuthenticate = true;
request.Credentials = networkCredential(sla);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = v_Timeout * 1000;
if (url.IndexOf("asmx") > 0 && parStartIndex > 0)
{
AppHelper.Logger.Append("#############" + sla.ServiceName);
using (StreamWriter reqWriter = new StreamWriter(request.GetRequestStream()))
{
while (true)
{
int index01 = parList.Length;
int index02 = parList.IndexOf("=");
if (parList.IndexOf("&") > 0)
index01 = parList.IndexOf("&");
string parName = parList.Substring(0, index02);
string parValue = parList.Substring(index02 + 1, index01 - index02 - 1);
reqWriter.Write("{0}={1}", HttpUtility.UrlEncode(parName), HttpUtility.UrlEncode(parValue));
if (index01 == parList.Length)
break;
reqWriter.Write("&");
parList = parList.Substring(index01 + 1);
}
}
}
else
{
request.ContentLength = 0;
}
response = (HttpWebResponse)request.GetResponse();