标签: httpclient

httpclient - 使用带有POST消息的cookie

我想创建一个小型Java应用程序,将一些wiki内容从一台服务器复制到另一台服务器.API 基于XML-RPC.

基本上我有三种方法login,getPageputPage.我使用Apache HttpClient 3.x并设法login成功登录并getPage正确地从旧维基获取页面.

使用cookie处理身份验证:我登录到新的wiki,并在相应的httpclient上设置了一些cookie.doku告诉我其中一个cookie用于验证.

然后我putPage在同一个httpclient上使用另一个POST方法执行,服务器响应身份验证失败消息.

代码序列就像这样(非常简化):

HttpClient client = new HttpClient();
PostMethod postLogin = createNewPostMethod("login", "user", "pw");
client.executeMethod(postLogin);
// Now I'm logged in and the client definitly has stored the cookies
PostMethod postPutPage = createNewPostMethod("putPage", getPage());
client.executeMethod(postPutPage);  // the server won't let me put the page
Run Code Online (Sandbox Code Playgroud)

它应该像那样工作,还是我必须手动将cookie添加到第二个帖子方法,如果是,如何?


编辑/解决方案

在这个问题的答案的帮助下,我能够识别并解决问题,这超出了httpclient的使用范围.最后,它是目标维基端的配置问题.这里的答案帮助我在另一个论坛中提出正确的问题.

java xml-rpc httpclient

1
推荐指数
1
解决办法
7210
查看次数

如何使用HttpClient进行WebDav调用?

具体来说,我想打电话给MKCOL通过HttpClient通过吊索REST API创建的Apache贾卡拉比特的文件夹.

我试过变种了

BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("MKCOL", restUrl);
Run Code Online (Sandbox Code Playgroud)

但到目前为止还没有骰子.我猜这不像我做的那么困难.

我也看到有MkColMethod类似的东西

MkColMethod mkColMethod = new MkColMethod(restUrl);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何利用它.我认为它可能适用于以前版本的HttpClient.我正在使用4.x.

java webdav httpclient jackrabbit sling

1
推荐指数
1
解决办法
3445
查看次数

HttpClient.execute总是给出Exception

我尝试使用Web的HTML,我使用此代码执行此操作:

HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("http://www.google.com");
    HttpResponse response;
    try {
        response = httpClient.execute(httpGet, localContext);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

并且总是在response = httpClient.execute(httpGet, localContext);它给我例外,也在我尝试的其他代码

java android httpclient

1
推荐指数
1
解决办法
5843
查看次数

使用Apache HttpClient定义源IP地址

我正在开发一个有以下需求的项目:

  • 使用源IP地址A访问远程服务器XX.YY.ZZ.WW上的http服务S.
  • 使用源IP地址B访问远程服务器XX.YY.ZZ.WW上的http服务T(与上面相同)

XX.YY.ZZ.WW是我无法控制的主人.

我的服务器在同一个以太网接口上配置了IP A和IP B. 我的项目使用Apache HttpClient.如有必要,可将其更改为其他内容.

根据我的TCP/IP知识,这很容易.只要我拥有IP,我应该能够将源IP地址更改为我想要的任何内容.但毕竟,我不是直接操纵IP数据包.我不知道如何使用HttpClient完成这项工作.

java http httpclient

1
推荐指数
2
解决办法
5191
查看次数

在scala中寻找处理重定向的http客户端

我在scala中寻找一个处理重定向的http客户端.如何在scala中获取Url的内容,处理重定向?

我看到了scala.io.Source示例,但他们没有处理重定向.

scala httpclient

1
推荐指数
1
解决办法
1617
查看次数

可移植类库上的WebProxy

我正在构建一个可移植类库项目.使用HttpClient类(从NuGet包安装).

现在.我想使用Proxy通过将HttpClientHandler传递给它构造函数来创建HttpClient(HttpClientHandler有一个Proxy属性,我们将为它分配一个WebProxy实例).问题是Portable Class Library不支持WebProxy类.它只有IWebProxy interace.

我在Google,NuGet Package上搜索过,但我找不到任何解决此案例的方法.请告诉我.我该如何解决这个问题(或者使用代理来制作HttpClient的另一种方法)

c# proxy portability httpclient

1
推荐指数
1
解决办法
1340
查看次数

尝试使用HttpClient上传文件时,Web Api的RequestEntityTooLarge响应

我正在尝试使用Asp.Net Web Api创建一个通过Post上传文件的Web服务.这些分别是Client和Web Api的实现:

客户:

using (var client = new HttpClient()) {
    client.BaseAddress = new Uri("https://127.0.0.1:44444/");
    using (var content =
       new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) {
       content.Add(new ByteArrayContent(File.ReadAllBytes(filepath)));
       content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
       var response = await client.PostAsync("api/Synchronization", content);

       if (response.IsSuccessStatusCode)
           eventLog1.WriteEntry("Synchronization has been successful", EventLogEntryType.Information);
       else 
           eventLog1.WriteEntry(response.StatusCode + ":" + response.ReasonPhrase, EventLogEntryType.Error);

      }
}
Run Code Online (Sandbox Code Playgroud)

服务器:

public class SynchronizationController : ApiController {

    public HttpResponseMessage SynchronizeCsv() {
        var task = this.Request.Content.ReadAsStreamAsync();
        task.Wait();
        Stream requestStream = task.Result;

        try {
            Stream fileStream = File.Create(HttpContext.Current.Server.MapPath(path));
            requestStream.CopyTo(fileStream); …
Run Code Online (Sandbox Code Playgroud)

c# httpclient http-status-code-404 asp.net-web-api

1
推荐指数
1
解决办法
6630
查看次数

如何在C#HttpClient中将调用循环到分页URL以从JSON结果下载所有页面

我的第一个问题,所以请善待...... :)

我正在使用C# HttpClient来调用Jobs API Endpoint.

这是端点:Jobs API Endpoint(不需要密钥,你可以点击它)

这给了我这样的JSON.

{
  "count": 1117,
  "firstDocument": 1,
  "lastDocument": 50,
  "nextUrl": "\/api\/rest\/jobsearch\/v1\/simple.json?areacode=&country=&state=&skill=ruby&city=&text=&ip=&diceid=&page=2",
  "resultItemList": [
    {
      "detailUrl": "http:\/\/www.dice.com\/job\/result\/90887031\/918715?src=19",
      "jobTitle": "Sr Security Engineer",
      "company": "Accelon Inc",
      "location": "San Francisco, CA",
      "date": "2017-03-30"
    },
    {
      "detailUrl": "http:\/\/www.dice.com\/job\/result\/cybercod\/BB7-13647094?src=19",
      "jobTitle": "Platform Engineer - Ruby on Rails, AWS",
      "company": "CyberCoders",
      "location": "New York, NY",
      "date": "2017-04-16"
    }
 ]
}
Run Code Online (Sandbox Code Playgroud)

我已经粘贴了一个完整的JSON代码段,因此您可以在答案中使用它.完整的结果真的很长.

这是C#类.

using Newtonsoft.Json;
using System.Collections.Generic;

namespace MyNameSpace
{
    public class DiceApiJobWrapper
    {
        public int count { …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous httpclient json.net task-parallel-library

1
推荐指数
1
解决办法
4338
查看次数

如何在Typescript类中创建Angular 5 HttpClient实例

我正在写一个包含httpClient的基类。它用于进行REST api调用。如果在构造函数中定义了httpClient变量,则设置正确,但在私有变量中则未设置。

这是我的示例代码:

@Injectable()
export class MyBaseClass implements {
  private httpClient = HttpClient

  constructor(
    private httpClient2: HttpClient
  ) {
    console.log("httpClient2", httpClient2)
    console.log("httpClient2.get", httpClient2.get)
  }
  callApi() {
    console.log("httpClient", this.httpClient)
    console.log("httpClient.get", this.httpClient.get)
  }
}
Run Code Online (Sandbox Code Playgroud)

构造函数输出: 在此处输入图片说明

callApi输出: 在此处输入图片说明

如您所见,两个变量并不相同,并且httpClient的get属性未定义。

我会在整个类中使用构造函数中的变量,但是我想要扩展此类,而在构造函数中使用变量并不方便。

任何帮助/建议将不胜感激。

谢谢,

get httpclient undefined typescript angular

1
推荐指数
1
解决办法
2761
查看次数

Spring Boot RestTemplate ResourceAccessException:POST请求上的I / O错误无法响应

我使用Spring Boot并在保持与第三方REST服务的长期连接的同时面临以下问题:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:5000/products/10": localhost:5000 failed to respond; nested exception is org.apache.http.NoHttpResponseException: localhost:5000 failed to respond
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:732)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:435)
    at com.example.pipeline.domain.service.nlp.NLPService.getDocumentsInfoNew(NLPService.java:42)
    at com.example.pipeline.domain.batch.steps.NLPTasklet.execute(NLPTasklet.java:170)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:394)
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:308)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:141)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.http.NoHttpResponseException: localhost:5000 failed to respond
    at org.apache.http.impl.conn.DefaultHttpRespons
Run Code Online (Sandbox Code Playgroud)

此服务可以将连接保持一小时或更长时间,然后再返回结果。

我的RestTemplate …

spring httpclient resttemplate spring-boot

1
推荐指数
1
解决办法
9123
查看次数