zoukankan      html  css  js  c++  java
  • xmlhttprequest对象

    实际就是个在客户端可以直接发送 post或者get请求并且能接受返回值的对象(同步异步)

    常用的get请求是url,常用的post请求是submit然后action ,然后浏览器接受返回的值(html)

    然后 我们在服务器用C#也可以模拟这两种请求,客户端模拟就是xmlhttprequest对象了,然后再加js就变成ajax了.

    实质就是用一个客户端对象请求服务器得到返回值,

    而不是用传统的get和post方式(在http正常提交内部再做个局部的http提交),

    如果把xmlhttprequest对象归为js,那么ajax就是js调用http请求和js调用webservices大同小异

    模拟请求代码迟些补上

     http请求谁都可以请求,都可以得到返回值

    js--------------------------->

    C#-------------------------->

    url-------------------------->      请求http无状态协议---------(Request)----------------->http服务器    

    submit(action)------------>                                                              

    其它------------------------>

                                              <-----------------------------(Response)----------------返回值html,xml,string等等

     



    随便在网上找了几个C#的http请求,没调试,凑活看吧

    WebClient位于System.Net命名空间下,通过这个类可以方便的创建Http请求并获取返回内容。

    一、用法1 - DownloadData

    string uri = "http://coderzh.cnblogs.com";
    WebClient wc 
    = new WebClient();
    Console.WriteLine(
    "Sending an HTTP GET request to " + uri);
    byte[] bResponse = wc.DownloadData(uri);
    string strResponse = Encoding.ASCII.GetString(bResponse);
    Console.WriteLine(
    "HTTP response is: ");
    Console.WriteLine(strResponse);

    二、用法2 - OpenRead

    string uri = " http://coderzh.cnblogs.com";
    WebClient wc 
    = new WebClient();
    Console.WriteLine(
    "Sending an HTTP GET request to " + uri);
    Stream st 
    = wc.OpenRead(uri);
    StreamReader sr 
    = new StreamReader(st);
    string res = sr.ReadToEnd();
    sr.Close();
    st.Close();
    Console.WriteLine(
    "HTTP Response is ");
    Console.WriteLine(res);

    还有

    通过Post方式发送数据可以避免Get方式的数据长度限制
    下面采用WebClient来实现这个功能
    Web服务端可以是任何CGI但是要搞清楚Web端接受的编码,代码如下

                WebClient wc = new WebClient();
                StringBuilder postData 
    = new StringBuilder();
                postData.Append(
    "formField1=" + "表单数据一");
                postData.Append(
    "&formField2=" + "表单数据二");
                postData.Append(
    "&formField3=" + "表单数据三");
                
    //下面是GB2312编码
                byte[] sendData = Encoding.GetEncoding("GB2312").GetBytes(postData.ToString());
                wc.Headers.Add(
    "Content-Type""application/x-www-form-urlencoded");
                wc.Headers.Add(
    "ContentLength", sendData.Length.ToString());

                
    byte[] recData= wc.UploadData("http://www.domain.cn/services/DataImport1.asp","POST",sendData);

                
    //显示返回值注意编码
                MessageBox.Show(Encoding.GetEncoding("GB2312").GetString(recData));

    注意"表单数据x"中包含如 "&","=","+"时需要使用,
    HttpUtility.UrlEncode( "+++xxx为什么不编码也可以",Encoding.GetEncoding("GB2312")) 进行编码
    HttpUtility.UrlEncode(string) 默认使用UTF-8进行编码,因此使用 UrlEncode编码时并且字段里有中文,并且目标网站使用GB2312时,需要在UrlEncode函数中指明使用Gb2312
    这样上面的拼接代码可以修改为如下:

     postData.Append("formField1=" +  HttpUtility.UrlEncode("表单数据一",Encoding.GetEncoding("GB2312")));
     postData.Append("&formField2=" +  HttpUtility.UrlEncode("表单数据二",Encoding.GetEncoding("GB2312")));
    ................

    标准的协议基本在所有语言里都有client的
    比如http soap socket tcp/ip udp ftp 还有 邮件那两个
    基本在所有语言里都能调用(C# java js flash php c++等等)
    中间涉及到验证的问题就需要具体商讨了

     

  • 相关阅读:
    关于模式窗体的缓存问题的解决方案
    C# 读取网页
    C# 压缩文件
    C#实现反射调用动态加载的DLL文件中的方法
    在线程中修改窗体控件内容
    C# 启用双缓存,避免ListView控件加载数据时闪烁
    JBuilder2005破解方法
    C# combbox datatable 赋值
    今天感觉到秋凉了~
    烦人的流程图~~~
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/1494806.html
Copyright © 2011-2022 走看看