十五、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 Round #392 (Div. 2) - C
题目链接:http://codeforces.com/contest/758/problem/C 题意:给定N*M矩阵的教室,每个位置都有一个学生,Sergei坐在[X,Y],然后老师会问K个问题,对 ...
- MYSQL5.7版本sql_mode=only_full_group_by问题,重启有效的方法
1./etc/mysql/mysql.conf.d/mysqld.cnf 或者my.cnf 总之就是mysql的配置文件 2.查看当前的sql模式 select @@sql_mode; 3.添加语 ...
- Informatica参考
近日在做informatica项目时,要求参照informatica中原有的mapping方式实现.那么当知道源表或者目标表或者是映射时,如何快速的找出分散在不同的Subject的mapping或者是 ...
- jmeter 参数化1_User Parameters(用户参数)
参数化:是自动化测试脚本的一种常用技巧,可将脚本中的某些输入使用参数来代替,如登录时利用GET/POST请求方式传递参数的场景,在脚本运行时指定参数的取值范围和规则. 脚本在运行时,根据需要选取不同的 ...
- C#高级编程笔记(17至21章节)线程/任务
17 Visual Studio 2013 控制台用Ctrl+F5可以显示窗口,不用加Console.ReadLine(); F5用于断点调式 程式应该使用发布,因为发布的程序在发布时会进行优化, 2 ...
- ubuntu下安装pyaudio
首先安装依赖库,不安装依赖库会安装失败: sudo apt-get install libasound-dev portaudio19-dev libportaudio2 libportaudiocp ...
- Appium环境搭建(Appium库的安装)
Appium环境搭建 谷歌驱动和对照:注意:64位向下兼容,直接下载32位的就可以啦,亲测可用. https://blog.csdn.net/allthewayforward/article/deta ...
- BottomNavigationBar 自定义 底部导航条
在flutter中,BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold 组件的参数. BottomNav ...
- LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域
题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...
- U-Boot是什么
U-Boot U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.U-Boot的作用是系统引导.U-Boot从FADSROM.8xxROM.PPCBOOT逐 ...