zoukankan      html  css  js  c++  java
  • 十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误

    一、远程服务器返回错误: (400) 错误的请求错误

    捕获异常查看具体错误

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.IO;
    using System.Net;
    using System.Reflection;
    using System.Text;
    
    namespace Test
    {
    
        class Program
        {
    
            static void Main(string[] args)
            {
                string url = "http://www.baidus.com";
                string contentType = "application/x-www-form-urlencoded";
                int timeOut = 3000;
                string parms = "626";
                int encoding = 65001;
                HttpWebRequest(url, parms, encoding, contentType, timeOut);
            }
    
            public static void HttpWebRequest(string url, string parms, int encoding, string contentType = "application/x-www-form-urlencoded", int timeOut = 3000)
            {
                try
                {
                    string result = string.Empty;
                    var mRequest = (HttpWebRequest)WebRequest.Create(url);
                    //相应请求的参数
                    var data = Encoding.GetEncoding(encoding).GetBytes(parms);
                    mRequest.Method = "Post";
                    mRequest.ContentType = contentType;
                    mRequest.ContentLength = data.Length;
                    mRequest.Timeout = timeOut;
                    mRequest.KeepAlive = true;
                    mRequest.ProtocolVersion = HttpVersion.Version10;
                    //请求流
                    var requestStream = mRequest.GetRequestStream();
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
                    //响应流
                    var mResponse = mRequest.GetResponse() as HttpWebResponse;//此位置进入catch(403)
                    var responseStream = mResponse.GetResponseStream();//
                    if (responseStream != null)
                    {
                        var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
                        //获取返回的信息
                        result = streamReader.ReadToEnd();
                        streamReader.Close();
                        responseStream.Close();
                    }
                }
                catch (System.Net.WebException ex)
                {
                    string result = string.Empty;
                    //响应流
                    var mResponse = ex.Response as HttpWebResponse;
                    var responseStream = mResponse.GetResponseStream();
                    if (responseStream != null)
                    {
                        var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
                        //获取返回的信息
                        result = streamReader.ReadToEnd();
                        streamReader.Close();
                        responseStream.Close();
                    }
                    result = "获取数据失败,请重试!" + url + ex.ToString() + "         返回数据" + result;
                }
            }
    
        }
    
    }

      

  • 相关阅读:
    ElasticSearch 2 (15)
    ElasticSearch 2 (14)
    ElasticSearch 2 (13)
    ElasticSearch 2 (12)
    浅谈 Comet、SSE、WebSocket
    js快速将字符串数组转化为数字数组(互换)
    如何给自己的vue组件做一个提示
    顶级域名和子级域名之间的cookie共享和相互修改、删除
    前端工程化
    数据校验工具 data-police
  • 原文地址:https://www.cnblogs.com/fger/p/11100992.html
Copyright © 2011-2022 走看看