我想根据屏幕尺寸加载不同的主页。有人可以帮忙吗?
例如,对于屏幕大小 < 960 px,我想将默认登陆页面显示为 index1.html,对于屏幕大小 > 960 px,我想将默认登陆页面显示为 index2.html
提前致谢。
有时我会从几个网站得到一些乱码回复.
这是我的代码:
Stream responseStream = response.GetResponseStream();
buffer = new Byte[256];//
int bytesRead;
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
outStream.Write(buffer, 0, bytesRead);
//resp=resp+ .UTF8.GetString(buffer, 0, bytesRead);
resp=resp + Encoding.ASCII.GetString(buffer); //resp is string
}
Run Code Online (Sandbox Code Playgroud)
当我从www.google.co.in请求时,我在resp字符串中得到以下字符:
?\ B\0\0\0\0\0ερ}ÿ·F ?????????Ž?????? {7米??? OX吗?\ r?ÿ??? 33 ?? d; Y ???? N 0?
我该如何克服这个问题?它与编码有关吗?
我有一个像这样的方法
private bool VerbMethod(string httpVerb, string methodName, string url, string command, string guid, out HttpWebResponse response)
Run Code Online (Sandbox Code Playgroud)
我这样使用它
HttpWebResponse response;
if (VerbMethod("POST", "TheMethod", "http://theurl.com", "parameter1=a", theGuid, out response))
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string responseString = sr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
它返回一个bool来指定方法是否顺利,并在out参数中设置响应以获取数据.
我有时会超时,然后后续请求也会超时.我看到这个SO WebRequest.GetResponse锁定了吗?
它重新启动using关键字.问题是,用上面的方法签名我不知道该怎么做.
using与out参数?HttpWebResponse?我想使用谷歌API来创建Gmail用户帐户.我正在向服务器发送JSON请求以获取授权代码,但我在httpwebresponse中收到了这些错误: -
异常详细信息:System.Net.WebException:远程服务器返回错误:(400)错误请求
var request = (HttpWebRequest)WebRequest.Create(@"https://accounts.google.com/o/oauth2/auth");
request.Method = "POST";
request.ContentType = "text/json";
request.KeepAlive = false;
//request.ContentLength = 0;
using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"scope\":\"https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile\"," + "\"state\":\"%2Fprofile\"," + "\"redirect_uri\":\"http://gmailcheck.com/response.aspx\"," + "\"response_type\":\"code\"," + "\"client_id\":\"841994137170.apps.googleusercontent.com\"}";
streamWriter.Write(json);
// streamWriter.Flush();
//streamWriter.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
StreamReader responsereader = new StreamReader(response.GetResponseStream());
var responsedata = responsereader.ReadToEnd();
//Session["responseinfo"] = responsereader;
//testdiv.InnerHtml = responsedata;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在制作一个Web Crawler,我发现我的一个方法GetHTML非常慢,因为它使用StreamReader从HttpWebResponse对象中获取HTML字符串.
这是方法:
static string GetHTML(string URL)
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.Proxy = null;
HttpWebResponse Response = ((HttpWebResponse)Request.GetResponse());
Stream RespStream = Response.GetResponseStream();
return new StreamReader(RespStream).ReadToEnd(); // Very slow
}
Run Code Online (Sandbox Code Playgroud)
我用秒表进行了测试,并在YouTube上使用了这种方法.
Time it takes to get an HTTP response: 500 MS
Time it takes to convert the HttpWebResponse object to a string: 550 MS
Run Code Online (Sandbox Code Playgroud)
所以HTTP请求很好,只是ReadToEnd()这么慢.
是否有任何替代ReadToEnd()方法从响应对象中获取HTML字符串?我尝试使用WebClient.DownloadString()方法,但它只是一个使用流的HttpWebRequest的包装器.
编辑:尝试使用套接字,它更快:
static string SocketHTML(string URL)
{
string IP = Dns.GetHostAddresses(URL)[0].ToString();
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Parse(IP), 80));
s.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\n\r\n")); …Run Code Online (Sandbox Code Playgroud) 我向服务发出请求并收到xml响应,如下所示.但是,我正在尝试将响应值存储在Dictionary中(或存储变量中返回的值),而我似乎无法使其工作.
任何帮助将不胜感激.
收到的xml回复:
<?xml version="1.0"?>
<ncresponse NCERRORPLUS="!" BRAND="ABC" PM="CC" currency="GBP" amount="10" STATUS="9" ACCEPTANCE="test123" NCERROR="0" NCSTATUS="0" PAYID="39409494" orderID="92E5CE91">
</ncresponse>
Run Code Online (Sandbox Code Playgroud)
c#代码:
try
{
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response
Dictionary<string, string> respValues = new Dictionary<string, string>();
try
{
string body = String.Empty;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("iso-8859-1"));
body += reader.ReadToEnd();
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(body);
XmlNodeList …Run Code Online (Sandbox Code Playgroud) 我无法使用WebClient,在任何人建议之前,因为它使我的合法应用程序看起来像迈克菲病毒.所以请不要这么做.
我有一个存储在我的服务器上的binary.txt文件.它大约是1,240kb.但是,HttpWebRequest下载的随机数量从1,300kb到1,700kb.
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
byte[] buffer = new byte[1240];
int bytesRead = 0;
StringBuilder sb = new StringBuilder();
//FileStream fileStream = File.Create(@"tcontent.txt");
while ((bytesRead = httpResponseStream.Read(buffer, 0, 1240)) != 0)
{
sb.Append(Encoding.ASCII.GetString(buffer));
//fileStream.Write(buffer, 0, bytesRead);
}
File.WriteAllText(@"tcontent1.txt", sb.ToString());
Run Code Online (Sandbox Code Playgroud)
(服务器上的binary.txt文件的内容是ASCII,因此我也将Encoding字符串转换为ASCII).
这是我编码该文本文件的方式(在该服务器上)
我的文件基本上是这样的:
byte[] bytes = File.ReadAllBytes("binary.txt");
String encBytes = Encoding.ASCII.GetString(bytes);
File.WriteAllText(file("binary.txt"), encBytes);
Run Code Online (Sandbox Code Playgroud)
我联系了AV公司关于WebDownloader被视为C#中的一些恶意导入,但他们没有回复我,所以我被迫使用HttpWebRequest.
我有以下代码登录到POST http://www.160by2.com/logincheck.aspx?iamindian=这个网址,我的问题是我无法登录,当我使用Fiddler调试它时,我看不到ne cookie认为我正在使用CookieContainer类,这里我在c#中使用Windows应用程序
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.160by2.com/logincheck.aspx?iamindian=");
string PostData = string.Format("htxt_UserName={0}&txt_Passwd={1}&txt_pop=&s=&d=&cmdSubmit=&namenumber={2}&strclf=&strshareuser=&ucountry=&ucode=&ucity=&uregion=", txtMobile.Text, txtPassword.Text, "1");
CookieContainer cookie = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = "http://www.160by2.com";
request.CookieContainer = cookie;
StreamWriter sWriter = new StreamWriter(request.GetRequestStream());
sWriter.Write(PostData);
sWriter.Close();
request.GetResponse().Close();
//some more code is here for further posting but above code can't login so below code is also not working
Run Code Online (Sandbox Code Playgroud)
我跟着这个,发帖但是它对我没有帮助..请帮帮我,这里我的错误..
我正在使用C#编写ASP.NET MVC3.
Response.Redirect("http://www.google.com");和之间有什么区别Response.Write("REDIRECT=http://www.google.com");?
c# ×6
.net ×1
c#-4.0 ×1
css ×1
dictionary ×1
html ×1
json ×1
out ×1
performance ×1
post ×1
stream ×1
streamreader ×1
timeout ×1
using ×1
xml ×1
xmldocument ×1