我想使用HttpWebRequest或RestClient将PDF上传到CrossEngage平台。作为响应,我得到:308永久重定向,但是当我在POSTMAN上尝试此操作时,一切都很好(200 OK)。我不是在做邮递员在做什么?
//RestClient
byte[] dataBytes = System.IO.File.ReadAllBytes(path);
string converted = Convert.ToBase64String(dataBytes);
var client = new RestClient(Url+Action);
client.FollowRedirects = true;
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-xng-authtoken", AuthToken);
request.AddHeader("x-xng-apiversion", "2");
request.AddHeader("content-type", "application/pdf");
request.AddParameter("application/pdf", converted, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content;
//HttpWebRequest
try
{
byte[] dataBytes = System.IO.File.ReadAllBytes(path);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + Action);
//request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers.Add("X-XNG-ApiVersion", "2");
request.Headers.Add("X-XNG-AuthToken", AuthToken);
request.Method = method;
request.AllowAutoRedirect = true;
request.MaximumAutomaticRedirections = 1;
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.ContentLength = 0;
request.CookieContainer = new CookieContainer();
if (1 == 1)
{
request.ContentLength = dataBytes.Length;
request.ContentType = contentType;
using (Stream requestBody = request.GetRequestStream())
{
requestBody.Write(dataBytes, 0, dataBytes.Length);
}
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
catch (WebException webex)
{
string m = "";
using (HttpWebResponse response = (HttpWebResponse)webex.Response)
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
throw new Exception(reader.ReadToEnd());
}
}
我希望200 OK状态代码
预先感谢
更新:我找到了一个解决方案,所以它可能会对某人有所帮助。 只需调用“位置”响应标头中给出的位置即可。 特此完整的代码:
尝试 { byte [] dataBytes = System.IO.File.ReadAllBytes(path);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + Action);
//request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers.Add("X-XNG-ApiVersion", "2");
request.Headers.Add("X-XNG-AuthToken", AuthToken);
request.Method = method;
request.AllowAutoRedirect = false;
if (1 == 1)
{
request.ContentLength = dataBytes.Length;
request.ContentType = contentType;
using (Stream requestBody = request.GetRequestStream())
{
requestBody.Write(dataBytes, 0, dataBytes.Length);
}
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if ((int)response.StatusCode >= 300 && (int)response.StatusCode <= 399)
{
var uriString = response.Headers["Location"];
var apiClient = (HttpWebRequest)WebRequest.Create(uriString);
apiClient.ContentType = contentType;
apiClient.Method = method;
apiClient.AllowAutoRedirect = false;
apiClient.Accept = "*/*";
apiClient.Headers.Add("X-XNG-ApiVersion", "2");
apiClient.Headers.Add("X-XNG-AuthToken", AuthToken);
apiClient.ContentLength = dataBytes.Length;
using (Stream requestBody = apiClient.GetRequestStream())
{
requestBody.Write(dataBytes, 0, dataBytes.Length);
}
var r = apiClient.GetResponse();
}
response.Close();
}
return "";
}
catch (WebException webex)
{
string m = "";
using (HttpWebResponse response = (HttpWebResponse)webex.Response)
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
throw new Exception(reader.ReadToEnd());
}
}