然后在调试的过程中发现当数据量多的时候Request.InputStream的Position居然是在结束位置

时间:2021-11-07 03:09:51

public class KeyWordController : BaseController
{
private string listClassUrl = "http://192.168.1.171:8789/keywords/list_class";

public ActionResult List()
{
//这种方法是通过参数键值对的方法发送过去
//var para = new Dictionary<string, string>();
//para.Add("lang", "1");
//return Json(Post(listClassUrl, para));

//这种方法是整个json或者字符串发送过去
var json = @"{""lang"":1}";
return Json(Post(listClassUrl, json),JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 指定Post地点使用Get 方法获取全部字符串
/// </summary>
/// <param>请求后台地点</param>
/// <returns></returns>
private string Post(string url, Dictionary<string, string> dic)
{
string result = string.Empty;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

#region 添加Post 参数
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
#endregion

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}

/// <summary>
/// POST整个字符串到URL地点中
/// </summary>
/// <param></param>
/// <param></param>
/// <returns></returns>
public string Post(string Url, string jsonParas)
{
string strURL = Url;

//创建一个HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Post请求方法
request.Method = "POST";
//内容类型
//request.ContentType = "application/x-www-form-urlencoded";

//设置参数,并进行URL编码

string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);

byte[] payload;
//将Json字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的ContentLength
request.ContentLength = payload.Length;
//发送请求,获得请求流

Stream writer;
try
{
writer = request.GetRequestStream();//获取用于写入请求数据的Stream东西
}
catch (Exception)
{
writer = null;
Console.Write("连接处事器掉败!");
}
//将请求参数写入流
writer.Write(payload, 0, payload.Length);
writer.Close();//*请求流

//String strValue = "";//strValue为http响应所返回的字符流
HttpWebResponse response;
try
{
//获得响应流
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}

Stream s = response.GetResponseStream();

Stream postData = Request.InputStream;
StreamReader sRead = new StreamReader(s);
string postContent = sRead.ReadToEnd();
sRead.Close();

return postContent;//返回Json数据
}

POST方法提交数据,一种众所周知的方法:

html页面中使用form表单提交,接收方法,使用Request.Form[""]或Request.QueryString[""]来获取。

这里介绍此外一种POST方法和接收方法,就是将整个数据作为插手到数据流中提交和接收

接收方法:

Stream s = System.Web.HttpContext.Current.Request.InputStream; byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); return Encoding.UTF8.GetString(b);

只需要从input Stream中读取byte数据,,然后转为string,再解析即可。如果要答复响应动静只需要用:Response.Write()  输出即可(和普通的页面输出一样)。

今天在手机App测试接口的时候发明一个通过POST方法的接口 获取body中的参数一直为空,但是在数据量小的时候却可以获取到数据,开始怀疑是不是POST的长度有限制,然后在web.config中改削了一下maxRequestLength,如下

1

2

3

 

<system.web>

    <httpRuntime targetFramework="4.5" maxRequestLength="20480" />

  </system.web>

 

然后发明 获取的值还是为空,然后在调试的过程中发明当数据量多的时候Request.InputStream的Position居然是在结束位置,如下图

然后在调试的过程中发现当数据量多的时候Request.InputStream的Position居然是在结束位置