我在" request.GetResponse()"试图解决此错误,但没有运气。
我的代码如下:
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();
}
}
我正在使用Tableau Server在另一台PC上远程连接。针对同一问题提出了很多问题,但没有一个问题可以解决我的错误。
错误的堆栈跟踪是:
[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
答案 0 :(得分:0)
我遇到了同样的问题,做了很多研究。我相信这是您的JSON休息请求字符串的问题。您需要做的是确保使用双斜线正确转义所有特殊字符。示例而不是\ n确保您有\ \ n或代替\ t,\ \ t等。这应该可以解决您的问题。