为了账号安全,请及时绑定邮箱和手机立即绑定

请问这个cookiecollection该怎么转换成字符?

/ 猿问

请问这个cookiecollection该怎么转换成字符?

慕雪6442864 2019-08-17 11:11:04

cookiecollection怎么转换成字符


查看完整描述

4 回答

?
慕勒3428872

using System.Net;
using System.IO;
using System.Text;

class Test
{
[STAThread]
static void Main(string[] args)
{
//

// TODO: 在此处添加代码以启动应用程序

//

string url = "http://localhost/csdn2/1.asp";

// <%

// if request("aa")="zhuye" then session("ok")="ok"

// if session("ok")="ok" then

// response.write("登录")

// else

// response.write("没有登录")

// end if

// %>

string indata = "aa=zhuye";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合

HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
//新建一个HttpWebRequest

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流

myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象

HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse

myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection

Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出

myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示"登录"

//拿到了Cookie,再进行请求就能直接读取到登录后的内容了

myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*

//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证

myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//再次显示"登录"

//如果把*行注释调,就显示"没有登录"

}
}

查看完整回答
反对 回复 2019-08-18
?
jeck猫


字符串 是String类 这是一个对象
字符 是char 类型,是基本类型 和编码方式有关 一般情况下不要用char 类型

如果要将String类 转成 char [ ]数组,toCharArray() 可以用这个方法
例如:String s="xxxxxxx"; char [ ] chArr=s.toCharArray();
如果要取得String对象中某个字符 就用charAt(int index)方法
例如 char ch=s.charAi(1);

public class Test {
public static void main(String[] args) {
String s="123456";
char [] charArr=s.toCharArray();
for(int i=0;i<s.length();i++)
System.out.printf("%4s",charArr[i]);

char ch=s.charAt(5);
System.out.println("\n"+ch);

}
}


查看完整回答
反对 回复 2019-08-18
?
慕村225694

代码:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Net;
using System.IO;
using System.Text;

class Test
{
[STAThread]
static void Main(string[] args)
{
//

// TODO: 在此处添加代码以启动应用程序

//

string url = "http://localhost/csdn2/1.asp";

// <%

// if request("aa")="zhuye" then session("ok")="ok"

// if session("ok")="ok" then

// response.write("登录")

// else

// response.write("没有登录")

// end if

// %>

string indata = "aa=zhuye";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合

HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
//新建一个HttpWebRequest

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流

myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象

HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse

myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection

Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出

myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示"登录"

//拿到了Cookie,再进行请求就能直接读取到登录后的内容了

myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*

//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证

myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//再次显示"登录"

//如果把*行注释调,就显示"没有登录"

}
}

注释写得很明确了,不多罗嗦了。 补充:如果是以get方式登录的话,直接修改url就可以了,indate可以不写任何东西。(不要去修改 myHttpWebRequest.Method为GET),比如把asp文件修改为 if request.querystring("aa")="zhuye" then session("ok")="ok",只要修改url为 string url="http://localhost/csdn2/1.asp?aa=zhuye";即可。






查看完整回答
反对 回复 2019-08-18
?
慕仰8121524

using System.Net;
using System.IO;
using System.Text;

class Test
{
[STAThread]
static void Main(string[] args)
{
//

// TODO: 在此处添加代码以启动应用程序

//

string url = "http://localhost/csdn2/1.asp";

// <%

// if request("aa")="zhuye" then session("ok")="ok"

// if session("ok")="ok" then

// response.write("登录")

// else

// response.write("没有登录")

// end if

// %>

string indata = "aa=zhuye";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合

HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
//新建一个HttpWebRequest

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流

myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象

HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse

myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection

Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出

myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示"登录"

//拿到了Cookie,再进行请求就能直接读取到登录后的内容了

myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*

//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证

myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//再次显示"登录"

//如果把*行注释调,就显示"没有登录"

}
}





查看完整回答
反对 回复 2019-08-18

添加回答

回复

举报

0/150
提交
取消
意见反馈 邀请有奖 帮助中心 APP下载
官方微信