端口webbrowser控件,表单提交到httpwebrequest?

时间:2013-11-30 03:17:57

标签: c# httpwebrequest webbrowser-control

我想将网络浏览器表单提交移植到httpwebrequest。 它非常简单:

wb.Document.GetElementsByTagName("input")[0].SetAttribute("value", "image.png");
wb.Document.GetElementsByTagName("input")[1].SetAttribute("value", ImageToBase64(p.Image));
wb.Document.GetElementsByTagName("form")[0].InvokeMember("submit");

我尝试过以下操作,但图像文件以某种方式被破坏了......

string param = "name=image.jpg&file=" + ImageToBase64(p.Image);
post(param, "urlToPHPfileShown");

public string post(string param, string url)
{

     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
     string postArgs = param;

     req.Method = "POST";
     req.CookieContainer = new CookieContainer();

     req.ContentType = @"application/x-www-form-urlencoded";
     byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(postArgs);
     req.ContentLength = buffer.Length;
     req.AllowAutoRedirect = true;
     using (Stream reqStream = req.GetRequestStream())
     {
          reqStream.Write(buffer, 0, buffer.Length);
          reqStream.Close();
     }
      HttpWebResponse response = (HttpWebResponse)req.GetResponse();
      using (Stream respStream = response.GetResponseStream())
      {
          using (StreamReader sr = new StreamReader(respStream))
          {
               string s = sr.ReadToEnd();
               return s;
           }
      }
}

编辑: 我刚看了WireShark的帖子。 正在工作的那个似乎有很多%2F而不仅仅是/(斜杠)。 那么是否存在字符集问题?

0 个答案:

没有答案