Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All

OS:Windows XP
Server: IIS
Visual Studio 2010 (C#.net)

I have to create an C#.net web application which POST the data on particular URL(third party website) and give the response back to the original site.

As I did same task in Curl in PHP, by which I post all the data to the URL and got the final generated output.

But I am unable to get any clue that what would be the best approach to do the same in C#.net.
C#
string url="http://www.tempuri.com";

WebRequest request = WebRequest.Create(url);
request.ContentType = "text/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential("username", "password");

byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("my_workspace");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

WebResponse response = request.GetResponse();
TextBox8.Text = response.ToString();

But the above code is giving me the error:
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.


Regards
mahendra
Posted
Updated 18-Apr-12 4:01am
v2
Comments
masdhasdh 19-Apr-12 10:38am    
Hello All..

Thanks for reviewing my question.

Just want to add some basic things in very normal way.

I want to post the Data on third party URL which is again a local HTTPS web-portal.
after success/unsuccessful it print the data on browser.That I have to show on output on my website.

Please suggest..

Thanks again..

1 solution

You can try below code. I have not check it with SSL but it working to post data for http protocol.

C#
postData = "data=" + Server.UrlEncode(readStr);
       
        //----  Post data           

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        byte[] data = encoding.GetBytes(postData);
        //myRequest.Credentials = new NetworkCredential(strId, strpass);
        myRequest.ContentLength = data.Length;
        Stream newStream = myRequest.GetRequestStream();
        newStream.Write(data, 0, data.Length);
        newStream.Close();
        newStream.Dispose();

        HttpWebResponse httpWebResponse = (HttpWebResponse)myRequest.GetResponse();
        Stream responseStream = httpWebResponse.GetResponseStream();
        httpWebResponse.Close();
        responseStream.Close();
        responseStream.Dispose();
 
Share this answer
 
v2
Comments
masdhasdh 30-Apr-12 11:06am    
Hi Abdul Muheet, Thanks for reply. But I am getting below error on line:
byte[] data = Encoding.GetBytes(postData);

Error 1 An object reference is required for the non-static field, method, or property 'System.Text.Encoding.GetBytes(string)' \</pre> Thanks..
masdhasdh 30-Apr-12 11:40am    
Hi
I change the line :
FROM
byte[] data = encoding.GetBytes(postData);
TO
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData);

But now the error is changed..
ERROR::The remote server returned an error: (407) Proxy Authentication Required.

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