LuLu_BaBy
2017-04-19 07:56
采纳率: 33.3%
浏览 4.8k

c# https post 请求 如何传参?总是报400错误。

求大神,rest APi,url路径没有错,就是不知道参数错了没有,请大神指教。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • qq_21878463 2017-04-19 08:08
    已采纳

    byte[] byteArray = Encoding.UTF8.GetBytes(strxml.ToString());//需要传的参数
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("需要访问的地址"));
    webReq.Method = "POST";//GET-POST
    webReq.ContentType = "text/xml";//传的参数格式
    webReq.ContentLength = byteArray.Length;
    Stream newStream = webReq.GetRequestStream();
    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
    newStream.Close();
    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    resultxml = sr.ReadToEnd();
    sr.Close();
    response.Close();
    newStream.Close();

    点赞 评论
  • LuLu_BaBy 2017-04-19 07:58
    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true; //总是接受     
    }  
    

    public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary parameters,Encoding charset)

    {

    HttpWebRequest request = null;

    //HTTPSQ请求

    X509Certificate certificate = new X509Certificate();
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

    request = WebRequest.Create(url) as HttpWebRequest;

    request.ProtocolVersion = HttpVersion.Version10;

    request.Method = "POST";
    request.ContentType = "application/json";
    request.Accept = "application/json;";
    //如果需要POST数据

    if (!(parameters == null || parameters.Count == 0))

    {

    StringBuilder buffer = new StringBuilder();

    int i = 0;

    foreach (string key in parameters.Keys)

    {

    if (i > 0)

    {

    buffer.AppendFormat("&{0}={1}", key, parameters[key]);

    }

    else

    {

    buffer.AppendFormat("{0}={1}", key, parameters[key]);

    }

    i++;

    }

    byte[] data = charset.GetBytes(buffer.ToString());

    using (Stream stream = request.GetRequestStream())

    {

    stream.Write(data, 0, data.Length);

    }

    }

    return request.GetResponse() as HttpWebResponse;

    }

    点赞 评论
  • LuLu_BaBy 2017-04-19 08:02

    protected void Button1_Click(object sender, EventArgs e)
    {

        string url = "https://vraserver.mocha-dc.com/identity/api/tokens";
       // string url = "https://vraserver.mocha-dc.com/identity/api/tenants/dev/directories";
        Encoding encoding = Encoding.GetEncoding("utf-8");
        IDictionary<string, string> parameters = new Dictionary<string, string>();
        parameters.Add("username", "name");
        parameters.Add("password", "pwd");
        parameters.Add("tenant", "div");
    
       // Post1(url, parameters);
    
        HttpWebResponse response = CreatePostHttpResponse(url, parameters, encoding);
        //打印返回值  
        Stream stream = response.GetResponseStream();   //获取响应的字符串流  
        StreamReader sr = new StreamReader(stream); //创建一个stream读取流  
        string html = sr.ReadToEnd();   //从头读到尾,放到字符串html  
        Console.WriteLine(html);  
    }
    
    点赞 评论
  • LuLu_BaBy 2017-04-19 08:26

    非常感谢,拿到了,谢谢。

    点赞 评论

相关推荐 更多相似问题