【问题标题】:Send url with web client使用 Web 客户端发送 url
【发布时间】:2013-04-05 13:03:45
【问题描述】:

当我尝试在地址栏上点击以下链接时,响应为“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);
}

我该如何解决这个问题?

【问题讨论】:

    标签: c# asp.net webclient


    【解决方案1】:

    我想,你应该像这样使用 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

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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2012-06-24
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多