1

我是 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()
4

0 回答 0