相关疑难解决方法(0)

使用HttpWebRequest POST到外部服务器上的表单

我试图模拟POST到外部服务器上不需要任何身份验证的表单,并捕获包含结果页面的sting.这是我第一次这样做,所以我正在寻找一些帮助,我到目前为止.这就是表单的样子:

<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>
Run Code Online (Sandbox Code Playgroud)

这就是我的代码:

    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();
Run Code Online (Sandbox Code Playgroud)

目前,代码返回"Stream is not readable".

c# asp.net httprequest

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net ×1

c# ×1

httprequest ×1