简体   繁体   中英

External POST request on IIS 7

We need to take post parameter from the external form. Example:

1st application - Form

<form method="post">
    <input type="text" name="privet" value="TestValue" />
    <input type="submit" value="submit" />
</form>

1st application - Controller

[HttpPost]
public ActionResult Test(string privet)
{
    return Content("Answer: " + privet);
}

2nd application ( external ) -- Controller

WebRequest _wr = WebRequest.Create("http://SomeExternalDomain/Home/Test");
 _wr.Method = "POST";
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes("privet=SomeValue");
_wr.ContentType = "application/x-www-form-urlencoded";
_wr.ContentLength = byteArray.Length;
using (Stream dataStream = _wr.GetRequestStream())
{
    dataStream.Write(byteArray, 0, byteArray.Length);
}


using (HttpWebResponse _response = (HttpWebResponse)_wr.GetResponse())
{
    using (Stream _dataStream = _response.GetResponseStream())
    {
         using (StreamReader _reader = new StreamReader(_dataStream))
         {
             return _reader.ReadToEnd();
         }
    }
}

If we send Post within a single application, post data accepted, but from external application, post discarded. (Instead of POST, on the method comes GET withoud data).

On IIS logs registered POST request in both cases.

2014-02-20 13:59:45 ::1 POST /Home/Test ...

2014-02-20 14:12:41 192.168.15.18 POST /Home/Test ...


Where can be limited external post requests? Thanks!

Your code looks fine. It could be simplified to:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "privet", "SomeValue" },
    };
    byte[] result = client.UploadValues("http://SomeExternalDomain/Home/Test", values);
    return Encoding.UTF8.GetString(result);
}

but I guess that won't resolve the issue. You seem to have mentioned something about a GET request. Maybe your external application is a web application and you are redirecting the client browser after making the POST request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM