C# 提供HttpException异常的重定向
我正在创建web应用程序,重定向到另一个页面时遇到问题,如果我在另一个方法中使用重定向代码,那么它可以正常工作,但我想使用运行线程中的重定向代码,这会引发HttpExeption。请看一下此代码,并在我的runloop()中显示使用重定向代码的正确方法方法。提前谢谢C# 提供HttpException异常的重定向,c#,asp.net,web,httpresponse,C#,Asp.net,Web,Httpresponse,我正在创建web应用程序,重定向到另一个页面时遇到问题,如果我在另一个方法中使用重定向代码,那么它可以正常工作,但我想使用运行线程中的重定向代码,这会引发HttpExeption。请看一下此代码,并在我的runloop()中显示使用重定向代码的正确方法方法。提前谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using Syst
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using System.Threading.Tasks;
public partial class _Default : System.Web.UI.Page
{
static TcpListener listener;
static TcpClient client;
public StreamReader STR;
public StreamWriter STW;
public String receive = "kkkk";
public String text_to_send;
string Jsn;
string URI ;
string myParameters;
string URL1;
Thread backgroundThread2;
Thread backgroundThread;
int i, n = 0, k = 0, len = 0, len2 = 0;
public int readflag = 0, writeflag = 0, pre1 = 0, pre2 = 0, pre3 = 0, nopre = 0;
char[] ch = new char[100];
char[] ch1 = new char[100];
char[] ch2 = new char[100];
String test = null, s1, s2, s3;
String test1 = null;
string frame;
const int LIMIT = 5; //5 concurrent clients
protected void Page_Load(object sender, EventArgs e)
{
frame = Request.QueryString["frame"];
if (Request.QueryString["Frame"] != null)
Response.Write("From Page1 param1 value=" + Request.QueryString["frame"]);
}
public void Button1_Click(object sender, EventArgs e) //start server
{
listener = new TcpListener(IPAddress.Any, 8002);
listener.Start();
client = listener.AcceptTcpClient();
STR = new StreamReader(client.GetStream());
STW = new StreamWriter(client.GetStream());
STW.AutoFlush = true;
backgroundThread = new Thread(new ThreadStart(RunLoop));
backgroundThread.Start();
// Task.Delay(50000);
//redirect1();
// Response.Redirect(URL1);
}
public void Button2_Click(object sender, EventArgs e) //Redirect server
{
Jsn = "25";
string URI = "Otherpage.aspx?";
string myParameters = "jasonop="+ Jsn;
string URL1 = URI + myParameters;
Response.Redirect(URL1);
// Response.Redirect("Otherpage.aspx?jasonop=25");
// System.Diagnostics.Process.Start("http://localhost:85/shaktijason/1.php?jasonop=25");
}
public class Person
{
public string Name { get; set; }
}
public void RunLoop() //receive
{
NetworkStream stream = client.GetStream();
Byte[] bytes = new Byte[256];
String data = null;
int i,k=1;
string str = "";
JavaScriptSerializer js = new JavaScriptSerializer();
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
byte[] bHex = Encoding.ASCII.GetBytes(data);
// TextBox1.Text = data;
string json = "[{Name:'01030008000105C8'}]";
Person[] persons = js.Deserialize<Person[]>(json);
string name = persons[0].Name.ToString();
// STW.WriteLine("01030008000105C8")
STW.WriteLine(name);
string result = decodedata(data);
str = result;
k++;
if (k == 4) { break; }
}
// string returnJson = "[{Freq:'" + str + "'}]";
Jsn = GetDeviceJSON(str);
URI = "Otherpage.aspx?";
myParameters = "jasonop="+Jsn;
URL1 = URI + myParameters;
Response.Redirect(URL1,false);
// Response.Redirect("Otherpage.aspx");
//string URI = "http://localhost:85/Shaktijason/1.php";
//string myParameters = "jasonop=jsn";
//using (WebClient wc = new WebClient())
//{
// wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
// string HtmlResult = wc.UploadString(URI, myParameters);
// Response.Redirect(HtmlResult);
//}
//string JSON = Json;
//backgroundThread2.Start();
// backgroundThread.Abort();
//return js.Serialize(returnJson);
}
public string decodedata(string data)
{
ch1 = data.ToCharArray();
ch2 = data.ToCharArray();
int len1 = data.Count(char.IsLetter);
len2 = data.Count(char.IsNumber);
int add = len1 + len2;
while (ch1[k] != 'k')
{
ch2[k] = ch1[k];
k++;
}
string strng = new string(ch2, 0, k);
len = strng.Length;
string sub = data.Substring(k + 1);
len2 = sub.Length;
k = 0;
if (len == 1)
{
strng = "0" + strng.PadLeft(len, '0');
}
if (len2 == 1)
{
sub = "0" + sub.PadLeft(len2, '0');
}
char[] go = new char[20];
string final = strng + sub;
if (final.Equals("00b8"))
{
final = "0";
}
Decimal intAgain = long.Parse(final, System.Globalization.NumberStyles.HexNumber);
intAgain = intAgain / 100;
string final3 = intAgain.ToString();
// string final2 = new string(go);
return final3;
}
[WebMethod]
public static string GetValues(string values)
{
return values;
}
public string GetDeviceJSON(String str)
{
Device[] emps = new Device[] {
new Device()
{
Freq=str
}
//new employee()
//{
// id=102,
// name="dinesh",
// salary=100000
//}
};
return new JavaScriptSerializer().Serialize(emps);
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.Services;
使用System.Net.Sockets;
使用System.IO;
使用系统线程;
Net系统;
使用系统文本;
使用System.Web.Script.Serialization;
使用System.Threading.Tasks;
公共部分类\u默认值:System.Web.UI.Page
{
静态TcpListener侦听器;
静态TcpClient客户端;
公共图书馆;
公共图书馆;
公共字符串receive=“kkk”;
要发送的公共字符串文本;
字符串Jsn;
字符串URI;
字符串参数;
字符串URL1;
线程背景线程2;
线程背景线程;
int i,n=0,k=0,len=0,len2=0;
public int readflag=0,writeflag=0,pre1=0,pre2=0,pre3=0,nopre=0;
char[]ch=新字符[100];
char[]ch1=新字符[100];
char[]ch2=新字符[100];
字符串测试=null,s1,s2,s3;
字符串test1=null;
弦框;
const int LIMIT=5;//5个并发客户端
受保护的无效页面加载(对象发送方、事件参数e)
{
frame=Request.QueryString[“frame”];
if(Request.QueryString[“Frame”!=null)
Response.Write(“From Page1 param1 value=“+Request.QueryString[“frame”]);
}
public void按钮1\u单击(对象发送者,事件参数)//启动服务器
{
侦听器=新的TcpListener(IPAddress.Any,8002);
listener.Start();
client=listener.AcceptTcpClient();
STR=newstreamreader(client.GetStream());
STW=新的StreamWriter(client.GetStream());
STW.AutoFlush=true;
backgroundThread=新线程(新线程开始(RunLoop));
backgroundThread.Start();
//任务延迟(50000);
//重定向1();
//响应.重定向(URL1);
}
公共无效按钮2\u单击(对象发送方,事件参数)//重定向服务器
{
Jsn=“25”;
字符串URI=“Otherpage.aspx?”;
字符串myParameters=“jasonop=“+Jsn;
字符串URL1=URI+myParameters;
响应.重定向(URL1);
//重定向(“Otherpage.aspx?jasonop=25”);
//系统.诊断.进程.启动(“http://localhost:85/shaktijason/1.php?jasonop=25");
}
公共阶层人士
{
公共字符串名称{get;set;}
}
public void RunLoop()//接收
{
NetworkStream=client.GetStream();
字节[]字节=新字节[256];
字符串数据=null;
int i,k=1;
字符串str=“”;
JavaScriptSerializer js=新的JavaScriptSerializer();
//循环以接收客户端发送的所有数据。
而((i=stream.Read(bytes,0,bytes.Length))!=0)
{
//将数据字节转换为ASCII字符串。
数据=System.Text.Encoding.ASCII.GetString(字节,0,i);
byte[]bHex=Encoding.ASCII.GetBytes(数据);
//TextBox1.Text=数据;
字符串json=“[{Name:'01030008000105C8'}]”;
Person[]persons=js.反序列化(json);
string name=persons[0]。name.ToString();
//STW.WriteLine(“010300080000105C8”)
STW.WriteLine(名称);
字符串结果=解码数据(数据);
str=结果;
k++;
如果(k==4){break;}
}
//字符串returnJson=“[{Freq:'”+str+“}]”;
Jsn=GetDeviceJSON(str);
URI=“Otherpage.aspx?”;
myParameters=“jasonop=”+Jsn;
URL1=URI+myParameters;
重定向(URL1,false);
//重定向(“Otherpage.aspx”);
//字符串URI=”http://localhost:85/Shaktijason/1.php";
//字符串myParameters=“jasonop=jsn”;
//使用(WebClient wc=new WebClient())
//{
//wc.Headers[HttpRequestHeader.ContentType]=“application/x-www-form-urlencoded”;
//字符串HtmlResult=wc.UploadString(URI,myParameters);
//重定向(HtmlResult);
//}
//字符串JSON=JSON;
//backgroundThread2.Start();
//backgroundThread.Abort();
//return js.Serialize(returnJson);
}
公共字符串数据(字符串数据)
{
ch1=data.ToCharArray();
ch2=data.ToCharArray();
int len1=data.Count(char.istleter);
len2=data.Count(char.IsNumber);
int add=len1+len2;
while(ch1[k]!=“k”)
{
ch2[k]=ch1[k];
k++;
}
字符串strng=新字符串(ch2,0,k);
len=标准长度;
string sub=data.Substring(k+1);
len2=子长度;
k=0;
如果(len==1)
{
strng=“0”+strng.PadLeft(len,'0');
}
if(len2==1)
{
sub=“0”+sub.PadLeft(len2,'0');
}
char[]go=新字符[20];
字符串final=strng+sub;
如果(最终等于(“00b8”))
{
final=“0”;
}
Decimal intAgain=long.Parse(final,System.Globalization.NumberStyles.HexNumber);
intAgain=intAgain/100;
字符串final3=intAgain.ToString();
//字符串final2=新字符串(go);
返回final3;
}
[网络方法]
公共静态字符串GetValues(字符串值)
{
返回值;
}
公共字符串GetDeviceJSON(字符串str)
{
设备[]emps=新设备[]{
新设备()
{
频率=str
}
//新雇员()
//{
//id=102,
//name=“dinesh”,
//工资=100000
//}
};
返回新的JavaScriptSerializer().Serialize(emps);
}
}您正试图从后台线程访问
response
对象,而响应早已消失
页面停止呈现后,不允许访问
响应
对象或HttpContext
下的任何其他对象。使用web套接字或AJAX调用创建异步请求和响应。是!你是对的,我现在不是以线程的形式运行RunLoop()方法,我得到的结果是