使用 webservice 请求 uri 太长 [英] Request uri too long with webservice

查看:24
本文介绍了使用 webservice 请求 uri 太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过需要像这样的 uri 的网络服务进行创建:

I want to do a create via a webservice which needs a uri like this:

http://<url>/webservice.php?operation=<operation>&elementType=<elementType>&element=<element>&

我的问题是,元素是带有 html 正文的电子邮件的所有信息,大约 6000 个字符.
我想这样调用网址:

my problem is, element is all information of an email with html body, which is about 6000 characters.
I want to call the url like this:

var request = WebRequest.Create(urlToUse.ToString());
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = urlToUse.Length;

var requestStream = request.GetRequestStream();
var requestStreamWriter = new StreamWriter(requestStream);
requestStreamWriter.Write(urlToUse);
requestStreamWriter.Close();

var response = request.GetResponse();
var responseStream = response.GetResponseStream();
if (responseStream == null) return null;
var responseStreamReader = new StreamReader(responseStream);

var responseFromServer = responseStreamReader.ReadToEnd();

responseStreamReader.Close();
responseStream.Close();
response.Close();

但它在

var response = request.GetResponse();

并说 uri 太长.

我无法更改 url 的服务器最大长度,并且 webservice 需要 url 中的参数.
我还没有找到适合我的解决方案,因此不胜感激.

更新:
对于面临同样问题的任何人,对我有用的解决方案是将我的查询放入像

and says the uri is too long.

I can't change the server's max length of the url and the webservice needs the parameters in the url.
I haven't found a suitable solution for me yet so any help is appreciated.

Update:
For anyone facing the same issue, the solution that worked for me was to put my query into an byte-Array like

var encoding = new UTF8Encoding();
byte[] bytes = enconding.GetBytes((queryString));

并将其写入 webrequest 而不是我的 queryString

and writing that into the webrequest instead of my queryString

var stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);

推荐答案

您可以将数据放入请求的正文中,如下所示:

You can put data in the body of the request with something a little like this:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://<url>/webservice.php");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var jsonContent = JsonConvert.SerializeObject(new YourObject
    {
        // Pseudo code... Replace <...> with your values etc
        Operation = <YourOperation>,
        ElementType = <YourElementType>,
        Element = <YourElement>,
        // etc...
    });

    HttpResponseMessage response;
    using (HttpContent httpContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
    {
        response = await client.PostAsync("youroperation/", httpContent);
    }

    // Do something with response if you want
}

这是基于 JSON 的,但可以是您想要传递的任何内容...这是一个简单的示例,希望能让您了解如何继续.

This is JSON based but could be anything you want to pass... This is a simple example that will hopefully give you an idea of how you can proceed.

这篇关于使用 webservice 请求 uri 太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆