在本文中,您将会了解到关于asp.net–为特定页面指定MaxHttpCollectionKeys?的新资讯,同时我们还将为您解释能为特定用户在web页面间实现跨页传值的对象是的相关在本文中,我们将带
在本文中,您将会了解到关于asp.net – 为特定页面指定MaxHttpCollectionKeys?的新资讯,同时我们还将为您解释能为特定用户在web页面间实现跨页传值的对象是的相关在本文中,我们将带你探索asp.net – 为特定页面指定MaxHttpCollectionKeys?的奥秘,分析能为特定用户在web页面间实现跨页传值的对象是的特点,并给出一些关于Android 网络提交数据 (HttpConnection HttpClient) POST GET 方式、Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点、ASP.NET Core MVC中Controller的Action,默认既支持HttpGet,又支持HttpPost、ASP.NET Web API为单个路由定制IHttpControllerSelector的实用技巧。
本文目录一览:- asp.net – 为特定页面指定MaxHttpCollectionKeys?(能为特定用户在web页面间实现跨页传值的对象是)
- Android 网络提交数据 (HttpConnection HttpClient) POST GET 方式
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点
- ASP.NET Core MVC中Controller的Action,默认既支持HttpGet,又支持HttpPost
- ASP.NET Web API为单个路由定制IHttpControllerSelector
asp.net – 为特定页面指定MaxHttpCollectionKeys?(能为特定用户在web页面间实现跨页传值的对象是)
对于那些不知道这个变量是什么的人,可以解决这个问题:
http://support.microsoft.com/kb/2661403
基本上,如果您有很多表单键(超过1000),则在发回页面时会出现异常.您可以通过在web.config中指定来解决此问题:
有关此限制的几个问题,例如:
‘Operation is not valid due to the current state of the object’ error during postback
但是,我没有看到任何与为特定页面设置此相关的内容.
解决方法
也许更好的方法是构建一个少于1000个项目的表单.也许是以巫师的形式.或者可以通过将多个值组合到单个数据结构中并在服务器上反序列化它.
Android 网络提交数据 (HttpConnection HttpClient) POST GET 方式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget38"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textSize="18sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:textSize="18sp" />
<Button
android:id="@+id/btn_get"
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1000"
android:text="GET提交" />
<Button
android:id="@+id/btn_post"
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1000"
android:text="POST提交" />
<Button
android:id="@+id/btn_get_httpclient"
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1000"
android:text="HttpClient Get提交" />
<Button
android:id="@+id/btn_post_httpclient"
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1000"
android:text="HttpClient POST提交" />
</LinearLayout>
package com.pas.postdata;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import com.pas.htmlview.utils.StreamTools;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
private EditText et_username;
private EditText et_password;
private final int SHOWINFO = 0;
private final int CHANGEUI = 1;
private Handler handler = new Handler()
{
@Override
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case SHOWINFO:
ShowInfo(MainActivity.this, msg.obj.toString());
break;
case CHANGEUI:
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void click(final View view)
{
final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
{
Toast.makeText(this, "信息不可为空", Toast.LENGTH_LONG).show();
} else
{
new Thread()
{
@Override
public void run()
{
try
{
InputStream is = null;
// GET方式
if (view.getId() == R.id.btn_get)
{
is = getByHttpConnection(username, password);
} else if (view.getId() == R.id.btn_post)
{
is = postByHttpConnection(username, password);
} else if (view.getId() == R.id.btn_get_httpclient)
{
// HttpClient 方式
is = getByHttpClient(username, password);
} else if (view.getId() == R.id.btn_post_httpclient)
{
is = postByHttpClient(username, password);
}
final String res = StreamTools.StreamToString(is);
if (res != null)
{
// 不使用handler的另一种方式
// 这种方式也可以封装
runOnUiThread(new Runnable()
{
@Override
public void run()
{
ShowInfo(MainActivity.this, res);
}
});
} else
{
handler.sendMessage(getMsg(SHOWINFO, "失败"));
}
} catch (Exception e)
{
e.printStackTrace();
handler.sendMessage(getMsg(SHOWINFO, "获取失败"));
}
}
}.start();
}
}
private InputStream postByHttpConnection(final String username, final String password) throws MalformedURLException, IOException, ProtocolException
{
HttpURLConnection conn;
// POST方式
String path = "http://192.168.1.100:8080/ServletTest/Login";
URL post_url = new URL(path);
conn = (HttpURLConnection) post_url.openConnection();
conn.setRequestMethod("POST");
// 准备数据
String data = "username=" + username + "&password=" + password;
byte[] data_bytes = data.getBytes();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data_bytes.length + "");
// POST方式:浏览器将数据以流的方式写入服务器
conn.setDoOutput(true);// 允许向外部写入数据
OutputStream os = conn.getOutputStream();
os.write(data_bytes);
conn.setConnectTimeout(5000);
if (200 == conn.getResponseCode())
{
return conn.getInputStream();
}
return null;
}
private InputStream getByHttpConnection(final String username, final String password) throws UnsupportedEncodingException, MalformedURLException, IOException, ProtocolException
{
HttpURLConnection conn;
String path = "http://192.168.1.100:8080/ServletTest/Login" + "?username=" + URLEncoder.encode(username, "utf-8") + "&password=" + URLEncoder.encode(password, "utf-8");
URL get_url = new URL(path);
conn = (HttpURLConnection) get_url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
return conn.getInputStream();
}
private InputStream getByHttpClient(final String username, final String password) throws Exception
{
String path = "http://192.168.1.100:8080/ServletTest/Login" + "?username=" + URLEncoder.encode(username, "utf-8") + "&password=" + URLEncoder.encode(password, "utf-8");
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(path);
HttpResponse response = client.execute(httpGet);
if (200 == response.getStatusLine().getStatusCode())
{
return response.getEntity().getContent();
}
return null;
}
private InputStream postByHttpClient(final String username, final String password) throws Exception
{
String path = "http://192.168.1.100:8080/ServletTest/Login";
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(path);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse response = client.execute(httpPost);
if (200 == response.getStatusLine().getStatusCode())
{
return response.getEntity().getContent();
}
return null;
}
public Message getMsg(int what, Object obj)
{
Message msg = new Message();
msg.what = what;
msg.obj = obj;
return msg;
}
public void ShowInfo(Context context, String info)
{
Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
}
}
package com.pas.htmlview.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamTools
{
public static String StreamToString(InputStream is)
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1)
{
baos.write(buffer, 0, len);
}
is.close();
baos.close();
byte[] res = baos.toByteArray();
String tem=new String(res);
return new String(res);
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点
..
1.HttpClient
优点:apache httpclient高效稳定,有很多API
缺点:由于API太多,很难在不破坏兼容性的情况下对它进行升级和扩展,维护成本高,故android 开发团队不愿意在维护该库而是转投更为轻便的httpurlconnection
Apache HttpClient早就不推荐httpclient,5.0之后干脆废弃,后续会删除。6.0删除了HttpClient。Java开发用HttpClient,官方推荐Android开发用HttpUrlConnection。
2.HttpURLConnection
优点:HttpURLConnection是一种多用途、轻量极的HTTP客户端,使用它来进行HTTP操作可以适用于大多数的应用程序。虽然HttpURLConnection的API提供的比较简单,但是同时这也使得我们可以更加容易地去使用和扩展它。比较轻便,灵活,易于扩展。
缺点:在Android 2.2版本之前,HttpURLConnection一直存在着一些令人厌烦的bug。比如说对一个可读的InputStream调用close()方法时,就有可能会导致连接池失效了。那么我们通常的解决办法就是直接禁用掉连接池的功能。
在android 2.2及以下版本中HttpUrlConnection存在着一些bug,所以建议在android 2.3以后使用HttpUrlConnection,2.3之前使用HttpClient。
3.OkHttp
OkHttp是一个现代,快速,高效的Http client,是一个相对成熟的解决方案,支持HTTP/2以及SPDY(SPDY介绍网址:https://zh.wikipedia.org/wiki/SPDY,SPDY(发音如英语:speedy),一种开放的网络传输协议,由Google开发),它为你做了很多的事情。
OKHttp是Android版Http客户端。非常高效,支持SPDY、连接池、GZIP和HTTP缓存。
支持SPDY,可以合并多个到同一个主机的请求
OkHttp实现的诸多技术如:连接池,gziping,缓存等就知道网络相关的操作是多么复杂了。
OkHttp扮演着传输层的角色。
OkHttp使用Okio来大大简化数据的访问与存储,Okio是一个增强 java.io 和 java.nio的库。
OkHttp 处理了很多网络疑难杂症:会从很多常用的连接问题中自动恢复。如果您的服务器配置了多个IP地址,当第一个IP连接失败的时候,OkHttp会自动尝试下一个IP。
OkHttp还处理了代理服务器问题和SSL握手失败问题。
OkHttp是一个Java的HTTP+SPDY客户端开发包,同时也支持Android。需要Android 2.3以上
OKHttp是Android版Http客户端。非常高效,支持SPDY、连接池、GZIP和 HTTP 缓存。
默认情况下,OKHttp会自动处理常见的网络问题,像二次连接、SSL的握手问题。
如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
从Android4.4开始HttpURLConnection的底层实现采用的是okHttp
缓存响应避免重复的网络请求
目前,该封装库志支持:
• 一般的get请求
• 一般的post请求
• 基于Http的文件上传
• 文件下载
• 上传下载的进度回调
• 加载图片
• 支持请求回调,直接返回对象、对象集合
• 支持session的保持
• 支持自签名网站https的访问,提供方法设置下证书就行
• 支持取消某个请求
4.Volley
Volley是一个简化网络任务的库。他负责处理请求,加载,缓存,线程,同步等问题。它可以处理JSON,图片,缓存,文本源,支持一定程度的自定义。
Volley在Android 2.3及以上版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是HttpClient。
**Volley停止了更新,而OkHttp得到了官方的认可,并在不断优化。
因此我最终替换为了OkHttp**
....
ASP.NET Core MVC中Controller的Action,默认既支持HttpGet,又支持HttpPost
我们知道ASP.NET Core MVC中Controller的Action上可以声明HttpGet和HttpPost特性标签,来限制可以访问Action的Http请求类型(GET、POST等)。
那么默认情况下如果我们没有给Controller的Action声明任何标签,那Action支持的是什么类型的Http请求呢?
为此我们新建一个ASP.NET Core MVC项目,并且新建一个HomeController,它有两个Action,如下所示:
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public object ShowData()
{
return new { Message="Good job!" };
}
}
其中ShowData这个Action会返回一个Json对象,用来响应从客户端浏览器发送过来的Ajax请求,也就是Http请求。
此外我们给HomeController的Index这个Action定义了一个视图Index.cshtml,如下所示:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(function (){
$("#btnPost").click(function () {
$.ajax({
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "/Home/ShowData",
data: {},
success: function (data) {
alert(data.message);
},
error: function (xhr, ts, et) {
alert("Error");
}
});
});
$("#btnGet").click(function () {
$.ajax({
type: "GET",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "/Home/ShowData",
data: {},
success: function (data) {
alert(data.message);
},
error: function (xhr, ts, et) {
alert("Error");
}
});
});
});
</script>
</head>
<body>
<div>
<button id="btnPost">Post</button>
</div>
<div>
<button id="btnGet">Get</button>
</div>
</body>
</html>
该视图上,两个Html按钮btnGet和btnPost,分别用来向HomeController的ShowData这个Action,发送GET和POST类型的Ajax请求。
如果 Ajax请求成功被ShowData这个Action响应,那么页面会输出ShowData返回的message值“Good job!”。
如果 Ajax请求没有被ShowData这个Action响应,那么页面会输出错误消息“Error”。
下面我们就来测试下:
访问Url地址Home/Index,在浏览器中先点击页面上的btnGet按钮,发送GET请求,结果如下:
然后在浏览器中点击页面上的btnPost按钮,发送POST请求,结果如下:
可以看到当我们没有给HomeController中的ShowData这个Action上声明任何特性标签的时候,实际上就相当于既声明了HttpGet特性标签,又声明了HttpPost特性标签,ShowData会同时响应两种类型的Http请求,相当于如下代码:
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpGet]
[HttpPost]
public object ShowData()
{
return new { Message = "Good job!" };
}
}
但是如果我们只给ShowData上声明一个Http特性标签,那么其就只会响应一种类型的Http请求了,例如如果我们现在在ShowData上只声明HttpPost特性标签,如下所示:
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public object ShowData()
{
return new { Message = "Good job!" };
}
}
那么现在点击视图Index.cshtml上的btnGet按钮,会显示如下结果:
可以看到GET类型的Ajax请求没有被ShowData响应,最后显示错误消息报错了。
ASP.NET Web API为单个路由定制IHttpControllerSelector
在研究时我发现以下代码意味着在应用程序启动时替换IHttpControllerSelector,但它完全替换了默认的控制器选择器,这导致应用程序中的所有路由都使用我的自定义控制器选择器:
config.Services.Replace(typeof(IHttpControllerSelector),new CustomControllerSelector(config));
有没有办法为单个路由配置IHttpControllerSelector?
解决方法
然后只需使CustomControllerSelector继承自DefaultHttpControllerSelector并检查该标志:
>如果已设置,请继续使用自定义逻辑
>如果未设置,则返回base(DefaultHttpControllerSelector)
这是代码:
1)消息处理程序,设置标志
public class RouteSpecificHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) { request.Properties["UseCustomSelector"] = true; return base.SendAsync(request,cancellationToken); } }
2)仅将每个路由消息处理程序分配给特定路由(不要为其他路由运行)
config.Routes.MapHttpRoute( name: "MyRoute",routeTemplate: "api/dummy/{id}",defaults: new {controller = "Dummy",id = RouteParameter.Optional},constraints: null,handler: new RouteSpecificHandler { InnerHandler = new HttpControllerdispatcher(config) } );
3)尊重旗帜的自定义选择器:
public class CustomSelector : DefaultHttpControllerSelector { public CustomSelector(HttpConfiguration configuration) : base(configuration) { } public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { if (request.Properties.ContainsKey("UseCustomSelector") && request.Properties["UseCustomSelector"] as bool? == true) { //your logic goes here } return base.SelectController(request); } }
4)注册选择器:
config.Services.Replace(typeof(IHttpControllerSelector),new CustomSelector(config));
编辑
如果你不希望从DefaultHttpControllerSelector继承 – 那么直接实现IHttpControllerSelector,而不是调用base.SelectController(request)将旧选择器保存为类中的字段/属性
public class CustomSelector : IHttpControllerSelector { private HttpConfiguration _config; public IHttpControllerSelector PrevIoUsSelector {get; set;} public CustomSelector(HttpConfiguration configuration) { _config = configuration; } public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { if (request.Properties.ContainsKey("UseCustomSelector") && request.Properties["UseCustomSelector"] as bool? == true) { //your logic goes here } return PrevIoUsSelector.SelectController(request); } }
然后只需更改注册:
var prevIoUsSelector = config.Services.GetService(typeof(IHttpControllerSelector)) as IHttpControllerSelector; config.Services.Replace(typeof(IHttpControllerSelector),new CustomSelector(config) { PrevIoUsSelector = prevIoUsSelector});
关于asp.net – 为特定页面指定MaxHttpCollectionKeys?和能为特定用户在web页面间实现跨页传值的对象是的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Android 网络提交数据 (HttpConnection HttpClient) POST GET 方式、Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点、ASP.NET Core MVC中Controller的Action,默认既支持HttpGet,又支持HttpPost、ASP.NET Web API为单个路由定制IHttpControllerSelector的相关信息,请在本站寻找。
本文标签: