zoukankan      html  css  js  c++  java
  • Winform Post请求传递Json格式参数的写法

    注意的是,Json传递需用到Hashtable(哈希表)来添加参数,本人也试过用JObject添加页不行,感觉应该可以的不知道怎么回事,直接上代码,

    Hashtable ht = new Hashtable();
    ht.Add("agentCode", agentcode);
    ht.Add("agentPassword", agentpwd);
    ht.Add("body", body);

    HttpRequest.DoPost(registerUrl, ht);  //HttpRequest是自定义的一个类

    /// <summary> WebService:Post调用
    /// </summary>
    /// <param name="url">Webservice地址</param>
    /// <param name="paramsOfUrl">传入数据</param>
    /// <returns>返回结果</returns>
    public static string DoPost(string url, Hashtable paramsOfUrl)
    {
    if (url == null)
    {
    throw new Exception("WebService地址为空");
    }
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

    // 编辑并Encoding提交的数据
    byte[] data = GetJointBOfParams(paramsOfUrl);

    // 发送请求
    request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    Stream stream = request.GetRequestStream();
    stream.Write(data, 0, data.Length);
    stream.Close();

    // 获得回复
    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string result = reader.ReadToEnd();
    reader.Close();
    return result;
    }

    /// <summary> 拼接参数串----Get
    /// </summary>
    /// <param name="paramsOfUrl">参数项</param>
    /// <returns>返回字符串</returns>
    private static String GetJointSOfParams(Hashtable paramsOfUrl)
    {
    if (paramsOfUrl == null || paramsOfUrl.Count == 0) return String.Empty;
    // 编辑并Encoding提交的数据
    StringBuilder sbuilder = new StringBuilder();
    int i = 0;
    foreach (DictionaryEntry de in paramsOfUrl)
    {
    string value = ToHttpChar(de.Value.ToString());
    if (i == 0)
    {
    sbuilder.Append(de.Key + "=" + value);
    }
    else
    {
    sbuilder.Append("&" + de.Key + "=" + value);
    }
    i++;
    }
    return sbuilder.ToString();
    }

    /// <summary> 拼接参数串----Post
    /// </summary>
    /// <param name="paramsOfUrl">参数项</param>
    /// <returns>返回字节数组</returns>
    private static byte[] GetJointBOfParams(Hashtable paramsOfUrl)
    {
    // 编辑并Encoding提交的数据
    String stringJointOfParams = GetJointSOfParams(paramsOfUrl);
    byte[] data = new ASCIIEncoding().GetBytes(stringJointOfParams);
    return data;
    }

    /// <summary> 转义特殊字符
    /// </summary>
    private static string ToHttpChar(string value)
    {
    value = value.ToString().Replace("+", "%2B");
    //value = value.ToString().Replace(" ", "%20");
    //value = value.ToString().Replace("/", "%2F");
    //value = value.ToString().Replace("?", "%3F");
    //value = value.ToString().Replace("%", "%25");
    //value = value.ToString().Replace("#", "%23");
    //value = value.ToString().Replace("&", "%26");
    //value = value.ToString().Replace("=", "%3D");
    //value = value.ToString().Replace(@"", "%5C");
    //value = value.ToString().Replace(".", "%2E");
    //value = value.ToString().Replace(":", "%3A");
    return value;
    }

  • 相关阅读:
    AX ERROR: Could not find my mock parent, most likely I am stale 不及格的程序员
    利用Segue在视图控制器间传值的问题 不及格的程序员
    Creating a Singleton Instance 不及格的程序员
    iPad 通知 UIKeyboardWillShowNotification 不会在keyBoard处在Undock状态下接到通知 不及格的程序员
    Why RootViewController's view is rotated Automatically by System when the app first loaded? 不及格的程序员
    如何弹出UIDatePicker最好 不及格的程序员
    jQuery开始做恶了 不及格的程序员
    what is the SEL,id and IMP,Class ,Method? 不及格的程序员
    Objectivec 字符串比较的陷井 不及格的程序员
    Unable to create any keyboard shortcuts after the iOS 6.1.3 update on iPad. 不及格的程序员
  • 原文地址:https://www.cnblogs.com/dachuang/p/9491876.html
Copyright © 2011-2022 走看看