senden GET-Anforderung, und immer versenden können ein content-body mit verb-Typ
Ich versuche eine eine GET-Anforderung senden, wie unten gezeigt, aber ich erhalte immer die folgende Fehlermeldung auf meiner Konsole:
Test_API_Access.Exception:Cannot send a content-body with this verb-type.
System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at Test_API_Access.Program.Test_API_Access() in c:\Users\<username>\Documents\Visual Studio 2013\Projects\Test_API_Access\Test_API_Access\Program.cs:line 43
Lese ich einen anderen verwandten thread auf Stack overflow, die hier erwähnt wird: Kann nicht senden Sie eine content-Körper mit diesem verb-Typ
Aber ich denke, ich habe mit mit (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
in meinem try-block die
hätte dieser Fehler vermieden werden?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.Runtime.Serialization;
using System.IO;
namespace Test_API_Access
{ class Program
{ static void Main(string[] args)
{ Test_API_Access();}
private static void Test_API_Access()
{ var publicKey = "publickeygoeshere";
var privateKey = "privatekeygoeshere";
var MyDateTime = DateTime.UtcNow.ToString();
var stringToSign = "POST" + "\n\n\n" + MyDateTime + "\n\n\n";
var signature = HmacSha1SignRequest(privateKey, stringToSign.ToString());
var resTreq = new StringBuilder();
var Url = "https://exvvii.com/webservice/testapi/123456";
try
{ //make web service call
byte[] dataStream = Encoding.UTF8.GetBytes(resTreq.ToString());
var webRequest = WebRequest.Create(Url);
webRequest.Method = "GET";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = dataStream.Length;
webRequest.Headers["datetime"] = MyDateTime;
webRequest.Headers["Authorization"] = publicKey + ":" + signature;
var newStream = webRequest.GetRequestStream(); //THIS IS LINE #43
newStream.Write(dataStream, 0, dataStream.Length);
long length = 0;
try
{ using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
{ length = response.ContentLength;
var header = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(header, encode);
Char[] read = new Char[256];
//Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);
while (count > 0)
{ //Dumps the 256 characters on a string and displays the string to the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
//Releases the resources of the response.
response.Close();
//Releases the resources of the Stream.
readStream.Close();
}
}
catch (WebException ex)
{
//Log exception and throw as for GET example above
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Test_API_Access.Exception:" + ex.Message);
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
public static string HmacSha1SignRequest(string privateKey, string valueToHash)
{ System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(privateKey);
HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
byte[] messageBytes = encoding.GetBytes(valueToHash);
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
var hashedValue = Convert.ToBase64String(hashmessage);
return hashedValue;
}
}
}
InformationsquelleAutor John | 2014-09-29
Du musst angemeldet sein, um einen Kommentar abzugeben.
Habe ich die Lösung gefunden, um dies für den Fall, jemand kommt über diese Fehlermeldung bei der Suche, im Grunde erhalten die Anfrage, stream, die Sie brauchen, um mit http-POST:
werden muss
Fand ich die Antwort hier: http://forums.asp.net/t/1706210.aspx?Cannot+send+a+Inhalt+body+mit+dieser+verb+type
InformationsquelleAutor darin
web.Header.Add("Header");
string Antwort = web.DownloadString("Ihr Uri || URL");
InformationsquelleAutor vinod Ba