Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中使用WebClient将参数发布到Azure服务URL#_C#_Azure_Post_Parameters - Fatal编程技术网

C# 如何在C中使用WebClient将参数发布到Azure服务URL#

C# 如何在C中使用WebClient将参数发布到Azure服务URL#,c#,azure,post,parameters,C#,Azure,Post,Parameters,我已经花了数小时测试/搜索了如何将C#中的参数发布到Azure服务,而没有得到错误405 string uri = "http://testservice.cloudapp.net/api/test"; string parameter = "value=1234"; using (WebClient wc = new WebClient()) { wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-u

我已经花了数小时测试/搜索了如何将C#中的参数发布到Azure服务,而没有得到错误405

string uri = "http://testservice.cloudapp.net/api/test";
string parameter = "value=1234";

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(uri, parameter);
}
< > C++中使用CHIKAT AT LB的代码工作精细
CkHttp http;    
CkHttpRequest req;
http.put_SessionLogFilename("c:/temp/httpLog.txt"); 
req.put_HttpVerb("POST");
req.put_Path("/api/test?value=1234");

CkHttpResponse *resp = http.SynchronousRequest("http://testservice.cloudapp.net",80,false,req);
if (resp == 0 )
    afxDump << http.lastErrorText() << "\r\n";

afxDump << resp->bodyStr() << "\r\n";
delete resp;

有没有提示我做错了什么?

您最好使用
HttpClient
而不是
WebClient
。通过查看C++代码的操作,C语言中的代码应该是这样的,使用<代码> HTTPcli客< /Calp> < /P>
    public void Test() {
        using (HttpClient client = new HttpClient()) {

        client.BaseAddress = new Uri("http://testservice.cloudapp.net");
        var response = client.PostAsync("api/test?value=1234", new StringContent(string.Empty)).Result;
        var statusCode = response.StatusCode;
        var errorText = response.ReasonPhrase;

        // response.EnsureSuccessStatusCode(); will throw an exception if status code does not indicate success

        var responseContentAsString = response.Content.ReadAsStringAsync().Result;
        var responseContentAsBYtes = response.Content.ReadAsByteArrayAsync().Result;
    }

}
下面是上面代码的异步版本

public async Task TestAsync() {
        using (HttpClient client = new HttpClient()) {

            client.BaseAddress = new Uri("http://testservice.cloudapp.net");
            var response = await client.PostAsync("api/test?value=1234", new StringContent(string.Empty));
            var statusCode = response.StatusCode;
            var errorText = response.ReasonPhrase;

            // response.EnsureSuccessStatusCode(); will throw an exception if status code does not indicate success

            var responseContentAsString = await response.Content.ReadAsStringAsync();
            var responseContentAsBYtes = await response.Content.ReadAsByteArrayAsync();
        }

    }

请尝试删除该内容类型标题,您拥有的代码是有效的。但我不确定webclient是如何将您的参数嵌入到请求中的,但我感觉该内容类型可能不正确。此外,这也是我在c+版本之间真正看到的唯一区别删除ContentType规范也会导致错误405。为什么首选httpclient?@Scott Selby,因为它是轻量级的,支持异步操作,并使您能够更好地控制请求/响应。它的实现是为了在考虑web API的情况下使用它。例如,如果您正在访问ASP.NET WebApi,这是默认路径。@Heribert Scharnagl如果您是在服务器端构建API的人,为什么需要在此处发布。我认为这应该只是一个简单的GET请求,因为您在查询字符串中提供参数。@Mihail Shishkov:我的帖子将在数据库中添加一些数据,使用PUT我更改它们,使用GET我读取值WebClient有解决方案吗?我不得不使用.NET4.0,但没有可用的HttpClient。