1

当我尝试地址栏上的以下链接 url 时,响应为“OK”

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

但是当我尝试像下面这样与 webclient 链接时,响应是“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);
}

我怎么解决这个问题?

4

1 回答 1

2

我认为,您应该使用 DownloadString 而不是 UploadString(URI, myParameters) ,如下所示:

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

当你想打开一个需要授权的 URL 时,你可能必须这样做两次:

  • 首先使用 GET 只是为了打开一个会话并获取一个 cookie
  • 之后使用步骤 1 中的 cookie 进行 POST

[编辑]找到了这个例子:https ://stackoverflow.com/a/4740851/1758762

祝你好运!

于 2013-04-05T13:19:47.597 回答