[英]HttpWebResponse-The remote server returned an error: (400) Bad Request
I am getting this error at "request.GetResponse()" trying to resolve this error but no luck. 我在尝试解决此错误但没有运气的“ request.GetResponse()”中遇到此错误。
My code is below: 我的代码如下:
string workbookPath = HttpContext.Current.Session["WorkBookPath"].ToString();
string wbName = Path.GetFileNameWithoutExtension(workbookPath);
string fileName = Path.GetFileName(workbookPath);
byte[] fileContents = File.ReadAllBytes(workbookPath);
var boundary = "xxxxxxxPOSTxxBOUNDARY";
System.IO.MemoryStream content = new MemoryStream();
StreamWriter writer = new StreamWriter(content);
writer.WriteLine("\r\n--" + boundary + "\r\n");
writer.WriteLine("Content-Disposition: name=\"{0}\";"
+ "\r\nContent-Type: {1}\r\n\r\n",
"request_payload",
"text/xml");
writer.WriteLine();
var xml = @"<tsRequest><workbook name=""" + wbName + "\" project id=\"Test" + projectId + "\" /></tsRequest>";
writer.WriteLine(xml);
// Include the file in the post data
writer.WriteLine("\r\n--" + boundary + "\r\n");
writer.WriteLine("Content-Disposition: name=\"{0}\";"
+ "filename=\"{1}\""
+ "\r\nContent-Type: {2}\r\n\r\n",
"tableau_workbook",
fileName,
"application/octet-stream");
writer.Flush();
// Read the file
FileStream fileStream = new FileStream(workbookPath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fileContents.Length];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
content.Write(buffer, 0, bytesRead);
}
fileStream.Close();
writer.WriteLine("\r\n--" + boundary + "--\r\n");
writer.Flush();
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8000/api/2.0/sites/" + siteId + "/workbooks?overwrite=false");
oRequest.Method = "POST";
oRequest.Headers["X-Tableau-Auth"] = HttpContext.Current.Session["authToken"].ToString();
using (var oStream = oRequest.GetRequestStream())
{
content.CopyTo(oStream);
content.Close();
}
using (var oResponse = (HttpWebResponse)oRequest.GetResponse())
{
using (var streamRdr = new StreamReader(oResponse.GetResponseStream()))
{
var response2 = streamRdr.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(response2);
oResponse.Close();
}
}
I am using Tableau Server to connect remotely on another PC. 我正在使用Tableau Server在另一台PC上进行远程连接。 A lot of questions are raised with the same issue but none of them resolves my error.
同一问题引发了很多问题,但是没有一个问题解决了我的错误。
The Stack Trace of the error is: 错误的堆栈跟踪为:
[WebException: The remote server returned an error: (400) Bad Request.]
System.Net.HttpWebRequest.GetResponse() +6518932
DashBoard.AddWorkBookToTabProject(String projectId, String siteId) in c:\Project\DashBoard.aspx.cs:876
RunRoutines.publishWorkbook() in c:\Project\RunRoutines.aspx.cs:76
RunRoutines.Page_Load(Object sender, EventArgs e) in c:\Project\RunRoutines.aspx.cs:55
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.<LoadRecursiveAsync>d__4.MoveNext() +228
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Web.Util.WithinCancellableCallbackTaskAwaiter.GetResult() +30
System.Web.UI.<ProcessRequestMainAsync>d__14.MoveNext() +4346
I had this same problem and did a lot of research. 我遇到了同样的问题,做了很多研究。 I believe this is a issue with your JSON rest request string.
我相信这是您的JSON rest请求字符串存在的问题。 What you need to do is make sure that all special characters are properly escaped with double slashes.
您需要做的是确保所有特殊字符都以双斜杠正确转义。 Example instead of \\n make sure you have \\ \\n or instead of \\t, \\ \\t etc. This should resolve your issue.
而不是\\ n的示例,请确保您具有\\ \\ n或代替\\ t,\\ \\ t等。这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.