标签: httpwebresponse

如何根据屏幕尺寸加载不同的主页

我想根据屏幕尺寸加载不同的主页。有人可以帮忙吗?

例如,对于屏幕大小 < 960 px,我想将默认登陆页面显示为 index1.html,对于屏幕大小 > 960 px,我想将默认登陆页面显示为 index2.html

提前致谢。

html css httpwebresponse adaptive-design

1
推荐指数
1
解决办法
1万
查看次数

如何以正确的字符串形式获取httpWebresponse的内容?

有时我会从几个网站得到一些乱码回复.

这是我的代码:

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?

我该如何克服这个问题?它与编码有关吗?

c# character-encoding httpwebresponse responsestream

0
推荐指数
1
解决办法
2万
查看次数

如何使用'out'和'out'参数

我有一个像这样的方法

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关键字.问题是,用上面的方法签名我不知道该怎么做.

  • 我应该手动调用最终处理吗?
  • 有没有一些方法仍然使用usingout参数?
  • 重写方法,所以它不暴露HttpWebResponse

c# timeout using out httpwebresponse

0
推荐指数
1
解决办法
323
查看次数

JSON POST请求到服务器,但服务器响应(400)错误请求

我想使用谷歌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)

c# post json httpwebrequest httpwebresponse

0
推荐指数
1
解决办法
6293
查看次数

C# - StreamReader.ReadToEnd()非常慢

我正在制作一个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)

.net performance stream httpwebresponse streamreader

0
推荐指数
1
解决办法
4542
查看次数

将Xml文档转换为C#Dictionary

我向服务发出请求并收到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)

c# xml xmldocument dictionary httpwebresponse

0
推荐指数
1
解决办法
2732
查看次数

HttpWebRequest无法正确下载文件

我无法使用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.

c# httpwebrequest character-encoding httpwebresponse

0
推荐指数
1
解决办法
4748
查看次数

无法在c#中使用HttpWebRequest设置cookie

我有以下代码登录到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# httpwebrequest httpwebresponse

-1
推荐指数
1
解决办法
4686
查看次数

Response.Redirect("http:// url")和Response.Write("REDIRECT = http:// url")之间有什么区别?

我正在使用C#编写ASP.NET MVC3.

Response.Redirect("http://www.google.com");和之间有什么区别Response.Write("REDIRECT=http://www.google.com");

httpwebresponse c#-4.0 asp.net-mvc-3

-2
推荐指数
1
解决办法
945
查看次数