1

When I tried to the following link url on address bar, response is "OK"

http://ww.exmaple.com.tr/webservices/addlead.php?first_name=" + r.Name + "&last_name=" + r.Surname + "&phone=" + r.Telephone + "&hash=" + r.HashCode

But when I tried to link with webclient like the below, response is "AUTH ERROR"

string URI = "http://ww.exmaple.com.tr/webservices/addlead.php";
string myParameters = "first_name=" + r.Name + "&last_name=" + r.Surname + "&phone=" + r.Telephone + "&hash=" + r.HashCode;

using (WebClient wc = new WebClient())
{
  wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
  string HtmlResult = wc.UploadString(URI, myParameters);
}

How can I solve this problem?

nermik
  • 1,485
  • 4
  • 16
  • 24

1 Answers1

2

I think, you shall use rather DownloadString instead of UploadString(URI, myParameters) like this:

string URI = "http://ww.exmaple.com.tr/webservices/addlead.php?";
string myParameters = "first_name=" + r.Name + "&last_name=" + r.Surname + "&phone=" + r.Telephone + "&hash=" + r.HashCode;

URI += myParameters;

using (WebClient wc = new WebClient())
{
 try
 {
  wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
  string HtmlResult = wc.DownloadString(URI);
 }
 catch(Exception ex)
 {
  // handle error
  MessageBox.Show( ex.Message );
 }
}

And when you want to open an URL needed authorization, you must maybe do it twice:

  • first with GET just to open a session and get a cookie
  • after that with POST using the cookie from step 1

[EDITED] Found the example for that: https://stackoverflow.com/a/4740851/1758762

Good luck!

Community
  • 1
  • 1
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92