首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HttpWebRequest.GetRequestStream()连接在可执行文件中被主动拒绝,但在WPF独立应用程序中不被拒绝

HttpWebRequest.GetRequestStream()连接在可执行文件中被主动拒绝,但在WPF独立应用程序中不被拒绝
EN

Stack Overflow用户
提问于 2019-01-19 01:18:45
回答 1查看 451关注 0票数 0

我正在使用web服务器通过HttpWebRequests调用它的应用程序接口。出于测试目的,我编写了一个独立的WPF应用程序,我的所有请求都运行正常。当我在我的生产应用程序中引用工作项目文件时,它现在返回请求正被服务器主动拒绝。

代码语言:javascript
运行
复制
public string Post(string xmlData, Transaction transaction)
{
        var result = "";

        try
        {
            var webReq = (HttpWebRequest)WebRequest.Create(BaseUrl);
            webReq.Accept = "application/xml";
            webReq.ContentType = "application/xml";
            webReq.Method = "POST";
            webReq.KeepAlive = false;
            webReq.Proxy = WebRequest.DefaultWebProxy;
            webReq.ProtocolVersion = HttpVersion.Version10;

            // If we passed in data to be written to the body of the request add it
            if (!string.IsNullOrEmpty(xmlData))
            {
                webReq.ContentLength = xmlData.Length;
                using (var streamWriter = new StreamWriter(webReq.GetRequestStream())) /**CONNECTION REFUSED EXCEPTION HERE**/
                {

                    streamWriter.Write(xmlData);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
            else //Otherwise write empty string as body
            {
                webReq.ContentLength = 0;
                var data = "";
                using (var streamWriter = new StreamWriter(webReq.GetRequestStream()))
                {

                    streamWriter.Write(data);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }

            //Attempt to get response from web request, catch exception if there is one
            using (var response = (HttpWebResponse)webReq.GetResponse())
            {
                using (var streamreader =
                    new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException()))
                {
                    result = streamreader.ReadToEnd();
                }
            }

            return result;
        }
        catch (WebException e)
        {
            //Handle web exceptions here               
        }
        catch (Exception e)
        {
            //Handle other exceptions here
        }
    }

还有没有人遇到过这个问题?

EN

Stack Overflow用户

回答已采纳

发布于 2019-01-19 03:54:36

在查看您的小提琴手请求后,我可以说原因可能是IP地址的差异。

第一次使用192.168.1.186:44000,第二次使用192.168.1.86:44000

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54258677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档