使用的HttpWebRequest张贴到形式的外服务器上 [英] Using HttpWebRequest to POST to a form on an outside server

查看:119
本文介绍了使用的HttpWebRequest张贴到形式的外服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模拟不需要任何身份验证,并捕获包含结果页面有刺的外部服务器上的POST到表单。这是我第一次这样做了,所以我要寻找一些帮助,我有什么至今。这是形式的样子:

I am trying to simulate a POST to a form on an external server that does not require any authentication, and capture a sting containing the resulting page. This is the first time I have done this so I am looking for some help with what I have so far. This is what the form looks like:

<FORM METHOD="POST" ACTION="/controller" NAME="GIN">
<INPUT type="hidden" name="JSPName" value="GIN">

Field1:
<INPUT type="text" name="Field1" size="30"
                maxlength="60" class="txtNormal" value=""> 

</FORM>

这是我的code样子:

This is what my code looks like:

    ASCIIEncoding encoding = new ASCIIEncoding();
    string postData = "Field1=VALUE1&JSPName=GIN";
    byte[] data = encoding.GetBytes(postData);
    // Prepare web request...
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://XXX/controller");
    myRequest.Method = "POST";
    myRequest.ContentType = "text/html";
    myRequest.ContentLength = data.Length;
    Stream newStream = myRequest.GetRequestStream();
    // Send the data.
    newStream.Write(data, 0, data.Length);

    StreamReader reader = new StreamReader(newStream);
    string text = reader.ReadToEnd(); 

    MessageBox.Show(text);

    newStream.Close();

目前,在code返回流未读。

Currently, the code returns "Stream was not readable".

推荐答案

您想读的响应流:

using (var resp = myRequest.GetResponse())
{
    using (var responseStream = resp.GetResponseStream())
    {
        using (var responseReader = new StreamReader(responseStream))
        {
        }
    }
}

这篇关于使用的HttpWebRequest张贴到形式的外服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆