C#中的Java(HttpPost,HttpGet,HttpPut,HttpPatch)方法

时间:2015-07-25 13:27:44

标签: java c# windows windows-phone-8.1 windows-8.1

以下是 JAVA 中的代码段。 我想在C#,Metro应用程序中做类似的事情。

public static String callWebservice(String paramJson) throws Exception {

    ...
    ...

    DefaultHttpClient httpclient = new DefaultHttpClient();

    ...
    if (requestType.equals("POST")  ) {
        HttpPost httpPostReq = new HttpPost(url);

        StringEntity se = new StringEntity(jsonParam.toString());
        se.setContentType("application/json;charset=UTF-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
        httpPostReq.setEntity(se);
        httpresponse = httpclient.execute(httpPostReq);
    } 

    else if (requestType.equals("GET") ) {
        HttpGet httpGetReq = new HttpGet(url);
        ...

    } 
    else if (requestType.equals("PUT")  ) {
        HttpPut httpPutReq = new HttpPut(url);
        ....

    } 
    else if (requestType.equals("PATCH")  ) {
        HttpPatch httpPatchReq = new HttpPatch(url);
        ...
    }

    responseText = EntityUtils.toString(httpresponse.getEntity());

    return responseText;

}

我想知道是否有单独的Java方法:(“ HttpPost ”,“ HttpGet ”,“ HttpPut ”,“ HttpPatch “)在C#中也是。

我尝试使用HttpClient完成任务但是我在添加标题“Accept”,“Content-Type”,“X-Security-AuthKey”时遇到错误并抛出错误,如:

  

错误的标题......

但不知怎的,我设法使用“ TryAddWithoutValidation ”添加标题。 在我的C#代码中,我有以下代码片段:

private async Task<string> CallHttpServiceHelper(string json)
    {
        try
        {
            ...
            HttpModel model = JsonConvert.DeserializeObject<HttpModel>(json);

            try
            {
                JsonObject jsonObject = new JsonObject();
                jsonObject["userName"] = JsonValue.CreateStringValue(model.paramss.userName);
                jsonObject["password"] = JsonValue.CreateStringValue(model.paramss.password);
                jsonObject["domain"] = JsonValue.CreateStringValue(model.paramss.domain);
                jsonObject["accessKey"] = JsonValue.CreateStringValue(model.paramss.accessKey);
                inputParams = jsonObject.Stringify();
            }
            catch (Exception ex)
            {
            }
            url = model.url;
            requestType = model.requesttype;

            try
            {
                authenticationKey = model.authenticationKey;
            }
            catch (Exception ex)
            {
            }

            if (requestType == "POST")
            {
                uri = new Uri(url);
                data = new StringContent(inputParams, Encoding.UTF8, "application/json");

                if (authenticationKey != null && authenticationKey != "")
                {
                    data.Headers.Add("X-Security-AuthKey", authenticationKey);
                }
                httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                httpClient.Timeout = TimeSpan.FromMinutes(10);
                var response = await httpClient.PostAsync(uri, data); 
                content = await response.Content.ReadAsStringAsync();
                httpClient.Dispose();

            }

            else if (requestType == "GET")
            {
                uri = new Uri(url);
                httpClient = new HttpClient();
                if (authenticationKey != null && authenticationKey != "")
                {
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-Security-AuthKey", authenticationKey);
                }
                httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                httpClient.Timeout = TimeSpan.FromMinutes(10);
                var response = await httpClient.GetAsync(uri);
                content = await response.Content.ReadAsStringAsync();
                httpClient.Dispose();                
            }

            else if (requestType == "PUT")
            {
                ...
            }

            else if (requestType == "PATCH")
            {
                ...
            }

            return content;
        }
        catch (Exception ex)
        {
            return "fail";
        }
    }

此代码在C#中,Windows RT Apps适用于 POST 方法。但是我没有在 GET 方法中从webservice获得所需的响应。在 GET 方法中,我必须通过仅传递三个Headers“Accept”,“Content-Type”,“X-Security-AuthKey”来调用WebService。 我不知道我做错了什么。

0 个答案:

没有答案