私はC#と.NETを初めて使用するため、HTTP POST要求を送信し、応答を取得してコンソールに書き込むプログラムを作成しようとしています。これが私のコードです:
static void Main()
{
string uri = "https://api0.ringrevenue.com:3000/api/2010-04-22/calls/1.xml";
string parameters = "start_time_t=133928918&call_center_call_id=91234567";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.KeepAlive = false;
byte[] bytes = Encoding.ASCII.GetBytes(parameters);
request.ContentLength = bytes.Length;
using (Stream os = request.GetRequestStream()) // this line gets the error
{
os.Write(bytes, 0, bytes.Length);
os.Close();
using (WebResponse response = request.GetResponse())
{
if (response == null) Console.WriteLine("Response is null");
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd().Trim());
Thread.Sleep(10000);
}
}
}
}
発生するエラーは、「WebExceptionが処理されませんでした」、「基になる接続が閉じられました:送信時に予期しないエラーが発生しました」です。何かご意見は?
スタックトレースは次のとおりです。
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at HttpPost.HttpPost.Main() in c:\users\support.qb-server\documents\visual studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\HttpPost.cs:line 24
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()