• 再谈HTTP Post XML数据


    之前写过一篇HTTPPost的文章http://www.cnblogs.com/qidian10/archive/2011/06/20/2085341.html

    这篇文章的代码其实是有点问题的:第一次提交数据会不通过,然后再试的话都是没有问题的,一直不解,直到今天在MSDN上发现了解决方案,贴出代码如下:

    protected string HttpPostData(string URL, string strParm, int outTime)
    {
    StringBuilder str
    = new StringBuilder();
    try
    {
    HttpWebRequest WRequest;
    HttpWebResponse WResponse
    = null;
    HttpWebResponse response
    = null;
    //preAuth the request
    // You can add logic so that you only pre-authenticate the very first request.
    // You should not have to pre-authenticate each request.
    Uri uri = new Uri(URL);
    WRequest
    = (HttpWebRequest)HttpWebRequest.Create(uri);
    WRequest.Credentials
    = new NetworkCredential("wcadmin", "wcadmin");
    WRequest.PreAuthenticate
    = true;
    WRequest.UserAgent
    = "Upload PLM XML";
    WRequest.Method
    = "HEAD";
    WRequest.Timeout
    = outTime;
    WResponse
    = (HttpWebResponse)WRequest.GetResponse();
    WResponse.Close();
    // Make the real request.
    WRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
    WRequest.Credentials
    = new NetworkCredential("wcadmin", "wcadmin");
    WRequest.PreAuthenticate
    = true;
    WRequest.UserAgent
    = "Upload PLM XML";
    WRequest.Method
    = "POST";
    WRequest.AllowWriteStreamBuffering
    = false;
    WRequest.Timeout
    = 10000;
    Byte[] postBytes
    = Encoding.UTF8.GetBytes(strParm);
    WRequest.ContentLength
    = postBytes.Length;
    Stream requestStream
    = WRequest.GetRequestStream();
    requestStream.Write(postBytes,
    0, postBytes.Length);
    requestStream.Close();
    try
    {
    response
    = (HttpWebResponse)WRequest.GetResponse();
    }
    catch (WebException e)
    {
    response
    = (HttpWebResponse)e.Response;
    }
    Stream responseStream
    = response.GetResponseStream();
    StreamReader streamReader
    = new StreamReader(responseStream, Encoding.Default);
    while (streamReader.Peek() != -1)
    {
    str.Append(streamReader.ReadLine());
    }
    streamReader.Close();
    response.Close();
    WResponse.Close();
    }
    catch (Exception error)
    {
    str.AppendLine(error.Message);
    }
    return str.ToString();
    }

    注意英文注释的那一段,通过这个方法即可成功提交数据,不会遇到莫名的400错误了。

    如果上面还是遇到问题,请参考下面的代码

    http://www.cnblogs.com/hyl8218/archive/2011/07/04/2097394.html

    要在发送请求的时候添加HTTP Basic Authentication认证信息到请求中,有两种方法:

    • 一是在请求头中添加Authorization:
      Authorization: "Basic 用户名和密码的base64加密字符串"
    • 二是在url中添加用户名和密码:
      http://userName:password@api.minicloud.com.cn/statuses/friends_timeline.xml

    下面来看下对于第一种在请求中添加Authorization头部的各种语言的实现代码。

    先看.NET的吧:

    string username="username";
    string password="password";
    //注意这里的格式哦,为 "username:password"
    string usernamePassword = username + ":" + password;
    CredentialCache mycache
    = new CredentialCache();
    mycache.Add(
    new Uri(url), "Basic", new NetworkCredential(username, password));
    myReq.Credentials
    = mycache;
    myReq.Headers.Add(
    "Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

    WebResponse wr
    = myReq.GetResponse();
    Stream receiveStream
    = wr.GetResponseStream();
    StreamReader reader
    = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();

      

    你当然也可以使用HttpWebRequest或者其他的类来发送请求。

  • 相关阅读:
    深入Spring之IOC之加载BeanDefinition
    Hexo+GitHub Actions 完美打造个人博客
    Spring中资源的加载原来是这么一回事啊!
    Web 跨域请求问题的解决方案- CORS 方案
    重新认识 Spring IOC
    Spring Data Jpa 入门学习
    前奏:Spring 源码环境搭建
    最短路径——floyd算法代码(c语言)
    leetcode 第184场周赛第一题(数组中的字符串匹配)
    如何用尾插法建立双链表(C语言,非循环)
  • 原文地址:https://www.cnblogs.com/qidian10/p/2105016.html
Copyright © 2020-2023  润新知