首页 \ 问答 \ 用HttpClient写入身体请求(Write in body request with HttpClient)

用HttpClient写入身体请求(Write in body request with HttpClient)

我想用XML内容类型编写请求的正文,但我不知道如何使用HttpClient Object( http://hc.apache.org/httpclient-3.x/apidocs/index.html )

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(this.url);
httpRequest.setHeader("Content-Type", "application/xml");

而且我不知道如何继续用我的XML写身体...


I want to write the body of a request with XML content-type but I don't know how with HttpClient Object ( http://hc.apache.org/httpclient-3.x/apidocs/index.html )

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(this.url);
httpRequest.setHeader("Content-Type", "application/xml");

And I don't know how to continue to write the body with my XML ...


原文:https://stackoverflow.com/questions/18188041
更新时间:2021-11-03 13:11

最满意答案

也许如果你明确你想做什么会更好。 你为什么要创建一个未命名的类? 它符合界面吗? 未命名的类是非常有限的,它们不能用作函数的参数,它们不能用作模板类型参数...

现在,如果您正在实现接口,则可以将引用传递给该接口:

class interface {
public:
   virtual void f() const = 0;
};
void function( interface const& o )
{
   o.f();
}
int main()
{
   class : public interface {
   public:
      virtual void f() const {
         std::cout << "bar" << std::endl;
      }
   } bar;
   function( bar ); // will printout "bar"
}

注意:对于将模板参数视为选项的所有答案,未命名的类不能作为模板类型参数传递。

C ++标准。 14.3.1,第2段:

2本地类型,没有链接的类型,未命名的类型或由这些类型中的任何一种组合的类型不得用作模板类型参数的模板参数。

如果您使用comeau编译器进行测试(该链接用于在线试用),您将收到以下错误:

错误:模板参数可能不引用未命名的类型

作为旁注,comeau编译器是我所知道的最符合标准的编译器,除了是我尝试过的最有用的错误诊断之一。

注意:Comeau和gcc(g ++ 4.0)使用上面的代码给出错误。 英特尔编译器(以及其他人的评论MSVS 2008)接受使用未命名的类作为模板参数,而不是标准。


Maybe it would be better if you explicit what you want to do. Why do you want to create an unnamed class? Does it conform to an interface? Unnamed classes are quite limited, they cannot be used as parameters to functions, they cannot be used as template type-parameters...

Now if you are implmenting an interface then you can pass references to that interface:

class interface {
public:
   virtual void f() const = 0;
};
void function( interface const& o )
{
   o.f();
}
int main()
{
   class : public interface {
   public:
      virtual void f() const {
         std::cout << "bar" << std::endl;
      }
   } bar;
   function( bar ); // will printout "bar"
}

NOTE: For all those answers that consider template arguments as an option, unnamed classes cannot be passed as template type arguments.

C++ Standard. 14.3.1, paragraph 2:

2 A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

If you test with comeau compiler (the link is for the online tryout) you will get the following error:

error: a template argument may not reference an unnamed type

As a side note, comeau compiler is the most standard compliant compiler I know of, besides being the one with the most helpful error diagnostics I have tried.

NOTE: Comeau and gcc (g++ 4.0) give an error with the code above. Intel compiler (and from other peoples comments MSVS 2008) accept using unnamed classes as template parameters, against the standard.

相关问答

更多
  • 在我通常工作的半大型项目(超过200万行代码)中,如果可以的话,我会禁止私人课程功能。 原因是一个私有类函数是私有的,但它在头文件中是可见的。 这意味着,如果我改变签名(或评论),无论如何,我有时会得到一个完整的重新编译,这个编译需要几分钟的时间(或者几个小时,取决于项目)。 只要对此说“不”并隐藏cpp文件中的私人内容即可。 如果我将从一个大的c ++项目开始,我会强制PIMPL Idiom: http ://c2.com/cgi/wiki?PimplIdiom将更多私人细节移动到cpp文件中。 In t ...
  • 你想在哪里放置除无名命名空间以外的本地类型? 类型不能像static一样具有链接说明符。 如果它们不是公开的,例如,因为它们是在一个标题中声明的,那么本地类型的名称就会相互冲突,例如,当两个翻译单元定义具有相同名称的类型时。 在这种情况下,您最终会遇到ODR违规。 在未命名的命名空间中定义类型消除了这种可能性。 要更具体些。 考虑你有 // file demo.h int foo(); double bar(); // file foo.cpp struct helper { int i; }; int ...
  • C ++标准读入7.3.1.1部分未命名的命名空间,第2段: 在命名空间范围内声明对象时,不推荐使用static关键字,未命名的命名空间提供了一个更好的选择。 静态仅适用于对象,函数和匿名联合的名称,不适用于键入声明。 编辑: 决定不推荐使用static关键字(影响翻译单元中的变量声明的可见性)已被反转( 参考 )。 在这种情况下,使用静态或未命名的命名空间本质上是完成相同事情的两种方式。 有关更多的讨论请看这个 SO问题。 未命名的命名空间仍然具有允许您定义翻译单位本地类型的优点。 请参阅这个 SO问题的 ...
  • 也许如果你明确你想做什么会更好。 你为什么要创建一个未命名的类? 它符合界面吗? 未命名的类是非常有限的,它们不能用作函数的参数,它们不能用作模板类型参数... 现在,如果您正在实现接口,则可以将引用传递给该接口: class interface { public: virtual void f() const = 0; }; void function( interface const& o ) { o.f(); } int main() { class : public interfa ...
  • 不幸的是,这是不可能的。 第9.3节/ 5: 如果成员函数的定义在词法定义之外是词法定义, 则成员函数名称应使用::运算符通过其类名限定 。 由于不存在类名,因此不可能有成员函数的类外定义。 GCC允许在此上下文中使用decltype-specifiers的事实是一个错误。 It is, unfortunately, impossible. §9.3/5: If the definition of a member function is lexically outside its class defini ...
  • click设置事件处理程序。 事件发生时,浏览器会调用click处理程序,并且e参数包含有关该事件的信息。 对于按键事件,它包含哪些按键被按下以及当时按下了哪些修饰符(移位,控制等)。 对于鼠标事件,它包含点击的位置和使用了哪个按钮。 有关事件结构的属性的更多信息,请参阅http://www.quirksmode.org/js/events_properties.html 。 click sets the event handler. The click handler gets called by the ...
  • 您需要声明该类的属性作为默认属性。 作为一个例子,下面是我写的一个String包装类的一部分: class StringClass private finished_ private data_ private size_ public function init (val) finished_ = cStr(val) set init = me end function public default property ge ...
  • 不,它不是完整的。 您无法单独从这些语法语句中派生语言语法。 标准文本中给出的额外要求也必须考虑在内。 在这种情况下,将是 7声明 ... 3在一个简单声明中 ,只有在声明一个类(第9章)或枚举(7.2)时,也就是说,当decl-specifier-seq包含一个类说明符时 ,可以省略可选的init-declarator-list ,带有类键 (9.1)的elaboratedtype说明符或枚举说明符 。 在这些情况下,只要decl-specifier-seq中存在类说明符或枚举说明符,这些说明符中的标识符 ...
  • 你有尝试使用的路径的问题 CLASSPATH C:\opencv\build\x86\vc10\lib;C:\opencv\buil­d\x86\vc10\staticlib;%CLASSPATH% PATH C:­\opencv\build\x86\vc10\bin\; you have problem with the path try to use CLASSPATH C:\opencv\build\x86\vc10\lib;C:\opencv\buil­d\x86\vc10\staticlib ...
  • 有人说它是未命名的对象 正确的术语是临时实例 。 他们中的一些人说它是构造函数。 它实际上是一个构造函数调用。 在变量声明语句之外的构造函数调用将创建Box的临时实例 ,并且该函数将返回一个值。 Some people say that it is unnamed object The correct term is temporary instance. Some of them say that it is constructor. It's in fact a constructor call. A ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。