ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "type=CVWeekly&html=" +strHtml + "&sign=" + strSign;
byte[] data = encoding.GetBytes(postData);
1.strHtml是抓取网页的源码,其中有不少&字符,会导致Request接收不完整。
2.抓取的网页是gb2312,测试程序是控制台应用程序,post提交到的aspx也是gb2312编码,开始为解决&字符的问题,用HttpUtility.UrlEncode编码后提交,但接到时解码,总是乱码。
客户端 js encodeURIComponent
服务端 C# Server.UrlEncode
HttpUtility.HtmlEncode不可以吗?
觉得可以这样操作
client.aspx中调用 UploadInfo("我的", "你的", "他的");
public static void UploadInfo(string type,string strHtml,string strSign)
{
WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
UTF8Encoding encoding = new UTF8Encoding();
string json = "{'type':'"+type+"','html':'" + strHtml + "','sign':'" + strSign+ "'}";
byte[] dataArray = encoding.GetBytes(json);
client.UploadData("http://localhost:4004/WebForm1.aspx?count=" + dataArray.Length, encoding.GetBytes(json));
}
protected void Page_Load(object sender, EventArgs e)
{
string json = DecodeInputStream(Request);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
UploadInfo info = javaScriptSerializer.Deserialize<UploadInfo>(json);
}
public static string DecodeInputStream(HttpRequest request)
{
Stream stream = request.InputStream;
stream.Seek(0, SeekOrigin.Begin);
int count = int.Parse(request.QueryString["count"]);
byte[] dataArray = new byte[count];
stream.Read(dataArray, 0, count);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetString(dataArray);
}
public class UploadInfo
{
public string Type { get; set; }
public string Html { get; set; }
public string Sign { get; set; }
}