十五、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 = ;
string parms = "";
int encoding = ;
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 = )
{
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, , 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;
}
} } }
十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误的更多相关文章
- WCF(远程服务器返回错误: 400 错误的请求)
类似相关问题有以下: WCF- restful接口 POST方式调用报错(远程服务器返回错误: 400 错误的请求) WCF Rest:不使用UriTemplate使用post方式传参解决HTTP40 ...
- post数据时报错:远程服务器返回错误: (400) 错误的请求。
网上查了多种方法,有不少说法,报400说是传的数据格式不对,最后的结论确实是数据格式不对. Content_Type为:application/json,配的数据格式有些麻烦,特别数多层,单层还好.例 ...
- SharePoint 2013 Workflow Manager 1.0 远程服务器返回错误: (400) 错误的请求。 不支持查询字符串中的 api-version
环境: Windows Server 2012 R2 Standard SharePoint Server 2013 with sp1 通过Web 平台安装程序 5.0,已安装 Workflow Ma ...
- system.net.webexception远程服务器返回了错误: NotFound。
Not Found类的错误主要是由于网络服务访问出错.所以需要分析是由哪个网络服务访问失败而导致的. DataAccessSilverlight.PowerDataServiceReference.G ...
- (C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误
因为工作需要调用WebService接口,查了下资料,发现添加服务引用可以直接调用websevice 参考地址:https://www.cnblogs.com/peterpc/p/4628441.ht ...
- 远程服务器返回错误:(414)Request-URI Too Large
近期因为疫情原因,一直是在家办公了,也导致了和同事对接接口上出现了很多小问题,这也从侧面反映出我个人对项目的设计不全面. 上面是对接接口时产生的一个问题:远程服务器返回错误:(414)Request- ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(十五)——集中式接口文档实现
之前有小伙伴在评论区留言说如何集成swagger,最开始没有想透给了对方一个似是而非的回答.实际上后来下来想了一下,用.NET5 提供的Source Generator其实可以很方便的实现接口集成.今 ...
- HttpWebResponse远程服务器返回错误: (500) 内部服务器错误。
现象 我们编码实现请求一个页面时,请求的代码类似如下代码: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl); req.Us ...
- NuGet在Push的时候提示“远程服务器返回错误:(403)已禁用”问题解决
在使用NuGet把包push到nuget官网的时候,提示了如下信息: Failed to process request. 'The specified API key is invalid or d ...
随机推荐
- Codeforces 722E 组合数学 DP
题意:有一个n * m的棋盘,你初始在点(1, 1),你需要去点(n, m).你初始有s分,在这个棋盘上有k个点,经过一次这个点分数就会变为s / 2(向上取整),问从起点到终点的分数的数学期望是多少 ...
- C++链接器
链接器把多个二进制的目标文件(object file)链接成一个单独的可执行文件 在链接过程中,它必须把符号(变量名.函数名等一些列标识符)用对应的数据的内存地址(变量地址.函数地址等)替代,以完成程 ...
- 第一节 :Windows 平台安装 MongoDB
MongoDB 下载 下载地址:https://www.mongodb.com/download-center#community 安装到C盘根目录下 创建数据目录 MongoDB将数据目录存储在 d ...
- centos 6.5 关闭防火墙
关闭防火墙分为临时关闭和永久关闭.临时关闭重启系统后恢复正常,永久关闭重启系统后仍然是关闭状态 临时关闭与开启 service iptables stop service iptables start ...
- kali网络源配置
使用vim对sources.list文件进行修改: $ vim /etc/apt/sources.list 随便挑选一个源添加到该文件中 ----------------------------- ...
- Python3解leetcode Maximum SubarrayHouse Robber
问题描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- Codeforces Round #578 (Div. 2) E. Compress Words (双哈希)
题目:https://codeforc.es/contest/1200/problem/E 题意:给你n个单词,你需要把他合成成一个句子,相邻的两个单词,相邻部分相同的话可以把其中一个的删掉 思路:因 ...
- Review: 9-13 July
9 July 并查集 int fa[]; for (int i=1; i<=n; ++i) fa[i]=i; int f(int x){return fa[x]==x?x:fa[x]=f(fa[ ...
- 2018-2019 2 20165203 《网络对抗技术》Exp7 网络欺诈防范
2018-2019 2 20165203 <网络对抗技术>Exp7 网络欺诈防范 实验目的 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 实验内容 (1)简 ...
- apr not found,APR-util not found,pcre,
1.下载所需软件包(此下载链接经由作者验证可使用): wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz wget http://arch ...