Web Services 在.NET C#中将XML发布到Web服务并接收响应

gdrx4gfi  于 2022-11-15  发布在  .NET
关注(0)|答案(2)|浏览(169)

我正在使用C#将XML文件发布到Web服务,但是当我请求响应“服务器错误- 500 -您不允许访问系统。任何帮助将不胜感激。

protected void Page_Load(object sender, EventArgs e)
    {
        WebRequest req = null;
        WebResponse rsp = null;
        try
        {
            string fileName = Server.MapPath("~\\test.xml");
            string uri = "http://212.170.239.71/appservices/http/FrontendService";
            req = WebRequest.Create(uri);
            //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
            req.Credentials = new NetworkCredential("myusername", "mypassword");
            req.Method = "POST";        // Post method
            req.ContentType = "text/xml";     // content type
            // Wrap the request stream with a text-based writer
            StreamWriter writer = new StreamWriter(req.GetRequestStream());
            // Write the XML text into the stream
            writer.WriteLine(this.GetTextFromXMLFile(fileName));
            writer.Close();
            // Send the data to the webserver
            rsp = req.GetResponse(); //I am getting error over here
            StreamReader sr = new StreamReader(rsp.GetResponseStream());
            string result = sr.ReadToEnd();
            sr.Close();
            Response.Write(result);

        }
        catch (WebException webEx)
        {
            Response.Write(webEx.Message.ToString());
            Response.Write(webEx.StackTrace.ToString());
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
            Response.Write(ex.StackTrace.ToString());
        }
        finally
        {
            if (req != null) req.GetRequestStream().Close();
            if (rsp != null) rsp.GetResponseStream().Close();
        }
    }
        //Function to read xml data from local system
  /// <summary>
  /// Read XML data from file
  /// </summary>
  /// <param name="file"></param>
  /// <returns>returns file content in XML string format</returns>
  private string GetTextFromXMLFile(string file)
  {
   StreamReader reader = new StreamReader(file);
   string ret = reader.ReadToEnd();
   reader.Close();
   return ret;
  }
oalqel3c

oalqel3c1#

500错误来自服务本身,这意味着您没有必要的访问权限,消息看起来像是自定义的,由服务的编写者返回,所以看起来像是您点击了它并得到了响应,但也许您的凭据是错误的?代码看起来是正确的-我首先要检查的是您传递的用户名和密码是否绝对正确。

q3aa0525

q3aa05252#

我遇到了同样的问题,并通过设置代理解决了它。下面是我的示例工作代码,它可能会对某些人有所帮助:)

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(WebRequestPath);
                myReq.Method = "POST";
                myReq.ContentType = "text/xml; encoding=utf-8";
                myReq.Timeout = 180000;
                myReq.KeepAlive = true;
                myReq.Headers.Add("SOAPAction", "http://tempuri.org/AmaliPostData");
                myReq.Accept = "gzip,deflate";
                byte[] PostData = Encoding.UTF8.GetBytes(xmlString.Trim());
                myReq.UseDefaultCredentials = false;
                NetworkCredential cred;
                cred = new NetworkCredential(WebRequestUname, WebRequestPassword);
                myReq.Credentials = cred;
                myReq.Host = WebRequestHost.Trim();
                myReq.Credentials = new System.Net.NetworkCredential(WebRequestUname, WebRequestPassword);
                myReq.PreAuthenticate = true;
                string SetProxy;
                SetProxy = WebRequestProxy; // something like this... "10.2.0.1:8080";
                var proxyObject = new WebProxy(SetProxy);
                myReq.Proxy = proxyObject;
                try
                {
                    var writer = myReq.GetRequestStream();
                    writer.Write(PostData, 0, PostData.Length);
                    writer.Close();
  }
                catch (Exception ex)
                {
                    WriteLog(" Writer Exception " + ex.Message + ex.InnerException + " host : " + myReq.Host);
                }

                HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
                string resp;
                using (var responseStream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(responseStream))
                    {
                        resp = sr.ReadToEnd();
                    }
                }

相关问题