0

I am posting XML file to Web services using C# , but I am getting error when I am requesting the response 'Server Error - 500 - You are not allowed to access the system . Any help will be appreciated.

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;
  }
4
  • If it is a web service, you can generate all of the client side code using the WSDL. Makes interfacing with it much easier Commented Feb 19, 2013 at 18:31
  • 2
    Can you right-click on the project in the solution explorer and click add service references, put in the endpoint for your service, click go, select a namespace, and hit ok? Commented Feb 19, 2013 at 19:57
  • Is it a SOAP web service? If so, then you need to use "Add Service Reference". Commented Feb 19, 2013 at 20:09
  • A couple of hints: your WebResponse, StreamReader, and StreamWriter all need to be in using blocks. Also, use ex.ToString() instead of displaying Message and StackTrace. You'll be missing any InnerException instances. Commented Feb 19, 2013 at 20:14

2 Answers 2

0

The 500 error is coming from the service itself, implying that you don't have the necessary access, the message looks like it's custom and returned by the writers of the service, so it looks like you're hitting it and getting a response, but perhaps your credentials are wrong? The code looks correct - the first thing I would check is that the username and password you're passing in are definitely right.

Sign up to request clarification or add additional context in comments.

1 Comment

Apparently I don't have the privs to comment on other posts yet :) but in answer to the service related messages:the service you're trying to hit appears to be a java servlet, and to add it as a project reference in the manner they describe you would need a WSDL file generated, which is an xml-like schema file that tells .Net how it can interact with the service. You will need to get a WSDL from the creator of the servlet, there are tools that will generate a WSDL for Java but I don't think they're 100% accurate. I'd check the credentials I mention above, as it seems like the problem to me.
0

I had the same problem and solved it by setting the Proxy. Here is my sample working code, it might help someone :)

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();
                    }
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.