我正在尝试发送一个GET请求,如下所示,但我继续获取,我的控制台上出现以下错误:
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
我在Stack overflow上读到了另一个相关的线程,这里提到了Cannot send a content-body with this verb-type
但我想,我一直在我的try块中使用(HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
可以避免这个错误吗?
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;
}
}
}
答案 0 :(得分:4)
如果有人在搜索时遇到此错误,我找到了解决方案,基本上是为了获取您需要使用http POST的请求流:
webRequest.Method = "GET";
需要
webRequest.Method = "POST";
我在这里找到答案:http://forums.asp.net/t/1706210.aspx?Cannot+send+a+content+body+with+this+verb+type
答案 1 :(得分:0)
WebClient web = New WebCLient();
web.Header.Add(“ Header”); 字符串响应= web.DownloadString(“您的Uri || URL”);
答案 2 :(得分:0)
请求内容仅在请求方法不同于Get
的情况下允许:
if (request.Method != HttpMethod.Get)
{
request.Content = ...;
}