我正的远程服务器返回错误:(400)错误的请求错误,而运行以下code。 我想上传XML文件的HTTP服务器上。 我的XML文件中包含标记为用户名,密码和域名,当我试图连接是手动,我能够进行连接,但使用相同的凭据,当我试图通过这个code将其连接,我得到400错误的请求错误。 请建议我如何克服这个问题。 谢谢 `
公共静态无效UploadHttp(XML字符串)
{
字符串为txtResults =的String.Empty;
尝试
{
字符串的URL =http://my.server.com/upload.aspx;
HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create(URL);
request.KeepAlive = FALSE;
request.SendChunked = TRUE;
request.AllowAutoRedirect = TRUE;
request.Method =邮报;
request.ContentType =为text / xml;
变种EN codeR =新UTF8Encoding();
VAR数据= EN coder.GetBytes(XML);
request.ContentLength = data.Length;
变种reqStream = request.GetRequestStream();
reqStream.Write(数据,0,data.Length);
reqStream.Close();
WebResponse的响应= NULL;
响应= request.GetResponse();
VAR读卡器=新的StreamReader(response.GetResponseStream());
VAR海峡= reader.ReadToEnd();
}
赶上(WebException前)
{
如果(ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse ERR = ex.Response为HttpWebResponse;
如果(ERR!= NULL)
{
串htmlResponse =新的StreamReader(err.GetResponseStream())ReadToEnd()。
为txtResults =的String.Format({0} {1},err.StatusDescription,htmlResponse);
}
}
其他
{
}
}
赶上(例外前)
{
为txtResults = ex.ToString();
}
}`
解决方案
您确定您应该使用POST不放?
POST通常用于应用程序/ x-WWW-urlen codeD
格式。如果您使用的是REST API,你也许应该是使用PUT?如果你上传的文件,你可能需要使用的multipart / form-data的
。并非总是如此,但通常,这是正确的事情。
另外你似乎没有使用凭据登录 - 你需要使用HttpWebRequest对象的凭据属性发送用户名和密码
。I am getting The remote server returned an error: (400) Bad Request error while running the following code. I am trying to upload xml file on the http server. My xml file contains tag for the username,password and domain and when i am trying to connect is manually i am able to connect it,but using same credentials when i am trying to connect it through this code, i am getting 400 Bad Request error. Please suggest me how to overcome this issue. Thanks `
public static void UploadHttp(string xml)
{
string txtResults = string.Empty;
try
{
string url = "http://my.server.com/upload.aspx ";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.KeepAlive = false;
request.SendChunked = true;
request.AllowAutoRedirect = true;
request.Method = "Post";
request.ContentType = "text/xml";
var encoder = new UTF8Encoding();
var data = encoder.GetBytes(xml);
request.ContentLength = data.Length;
var reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
WebResponse response = null;
response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var str = reader.ReadToEnd();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse err = ex.Response as HttpWebResponse;
if (err != null)
{
string htmlResponse = new StreamReader(err.GetResponseStream()).ReadToEnd();
txtResults = string.Format("{0} {1}", err.StatusDescription, htmlResponse);
}
}
else
{
}
}
catch (Exception ex)
{
txtResults = ex.ToString();
}
}`
解决方案
Are you sure you should be using POST not PUT?
POST is usually used with application/x-www-urlencoded
formats. If you are using a REST API, you should maybe be using PUT? If you are uploading a file you probably need to use multipart/form-data
. Not always, but usually, that is the right thing to do..
Also you don't seem to be using the credentials to log in - you need to use the Credentials property of the HttpWebRequest object to send the username and password.