Click here to Skip to main content
15,894,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I encountered a System.Net.WebException: The remote server returned an error: (403) Forbidden on this line
StreamWriter sw = new StreamWriter(request.GetRequestStream());

and this is the code that I try , Help please !

C#
public void call()
        {
            string requestString = HttpUtility.UrlPathEncode("idata=" + "C:/Users/DELL/Desktop/PFE/JobPostingTest/JobPostingTest.xml"); //XMLDoc is the XML data string being submitted.            

            string HUD_URL = "https://gateway.monster.com:8443/bgwBroker";  //Just pretend I have a valid url, username and password here.
            string uName = "xrtpjobsx01";
            string pWord = "rtp987654";

            
                HttpWebRequest request = null;
                WebResponse response = null;
                request = (HttpWebRequest)WebRequest.Create(HUD_URL); // Create a request using a URL that can receive a post. 
                request.Credentials = new NetworkCredential(uName, pWord); //Our credentials for the website.            
                request.Method = "POST";  // Set the Method property of the request to POST.                
                request.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";
                StreamWriter sw = new StreamWriter(request.GetRequestStream()); // Wrap the   request stream with a text-based writer                   
                sw.WriteLine(requestString);  // Write the xml as text into the stream
                sw.Close();
}
Posted
Updated 31-Mar-14 0:35am
v2

1 solution

You have no the proper right to access that web service (username or password are wrong?)...
http://en.wikipedia.org/wiki/HTTP_403[^]
 
Share this answer
 
Comments
amiach 31-Mar-14 6:39am    
Username and password are provided by Monster to test the request
amiach 31-Mar-14 6:45am    
I change the username and password but an other exception appreas on the line below :
response = request.GetResponse();
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

public void call()
{
string requestString = HttpUtility.UrlPathEncode("idata=" + "C:/Users/DELL/Desktop/PFE/JobPostingTest/JobPostingTest.xml"); //XMLDoc is the XML data string being submitted.

string HUD_URL = "https://208.71.198.74:8443/bgwBroker"; //Just pretend I have a valid url, username and password here.
string uName = "xtestxftp";
string pWord = "ftp12345";


HttpWebRequest request = null;
WebResponse response = null;
request = (HttpWebRequest)WebRequest.Create(HUD_URL); // Create a request using a URL that can receive a post.
request.Credentials = new NetworkCredential(uName, pWord); //Our credentials for the website.
request.Method = "POST"; // Set the Method property of the request to POST.
request.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";
StreamWriter sw = new StreamWriter(request.GetRequestStream()); // Wrap the request stream with a text-based writer
sw.WriteLine(requestString); // Write the xml as text into the stream
sw.Close();

response = request.GetResponse(); // Send the data to the webserver // Get the response.
if (request != null) request.GetRequestStream().Close(); //Close the request object

string responseFromServer = string.Empty;
if (response != null)
{
StreamReader incomingStreamReader = new StreamReader(response.GetResponseStream());
responseFromServer = incomingStreamReader.ReadToEnd(); // Put the response in a string
incomingStreamReader.Close();
response.GetResponseStream().Close();
//lbl_ResponseInfo.Text = ((HttpWebResponse)response).StatusDescription.ToString(); // Display the status.
}

XmlDocument xm = new XmlDocument();
xm.LoadXml(string.Format("<root>{0}", responseFromServer));
xm.Save(@"C:\Users\DELL\Documents\Visual Studio 2012\Projects\testXML\testXML\file.xml");
//showXML("C:/Users/DELL/Desktop/PFE/JobPostingTest/JobPostingTest.xml", responseFromServer);// Display the content.
}
Kornfeld Eliyahu Peter 31-Mar-14 6:50am    
https://gateway.monster.com:8443/bgwBroker publishing a soap service via wsdl.
All that you need to do is adding a web reference to that address to your project, create a new object from the proxy type, than call the desired function at your will.
What are you doing now is just not right for that kind of service...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900